2016-04-08 12 views
-1

Ich habe einen Code geschrieben, um meinen Player zu steuern. Die Jump + Jump Animation (zusammen mit den Kleidungs- und Haaranimationen) spielt, wenn der Charakter die Leertaste berührt und wenn der Spieler von einer Plattform fällt.Double Jump funktioniert nicht

Mein Doppelsprung funktioniert nicht. Mein Spieler kann nur einmal springen. Kann jemand sehen, warum das nicht funktioniert?

using UnityEngine; 
using System.Collections; 

public class playerController : MonoBehaviour { 
    private Animator anim; 
    public Vector3 moveForward = new Vector3(1,0,0); 
    private float speed = 4; 
    private bool isGrounded; 
    private Rigidbody rb; 


    private bool doubleJump; 

    // Use this for initialization 
    void Start() { 
     rb = GetComponent<Rigidbody>(); 
     anim = GetComponent<Animator>(); 
     doubleJump = true; 
    } 

    // Update is called once per frame 
    void Update() { 
     transform.Translate(Vector3.forward * speed * Time.deltaTime); 


     if(isGrounded) { 

      if(Input.GetKeyDown("space")) { 
       if(isGrounded) 
       { 
       rb.velocity = new Vector3(10, 15, 0); 
       anim.Play("rig|Character_ActionsJUMP", -1, 0f); 
       anim.Play("Dress_Armature |Jump", 1, 0f); 
       anim.Play("Hair_Armature|Jump", 0, 0f); 
       isGrounded = false; 

       } 

       else if (!isGrounded && doubleJump) { 

       rb.velocity = new Vector3(10, 15, 0); 
       anim.Play("rig|Character_ActionsJUMP", -1, 0f); 
       anim.Play("Dress_Armature |Jump", 1, 0f); 
       anim.Play("Hair_Armature|Jump", 0, 0f); 
       doubleJump = false; 

       } 
      } 
      } 
    } //endOFUpdate 

    void OnCollisionEnter(Collision other) { 
      if(other.gameObject.tag == "Ground") { 
      isGrounded = true; 
      doubleJump = true; 
      anim.Play("rig|Character_Run", -1, 0f); 
      anim.Play("Dress_run", 1, 0f); 
      anim.Play("Hair_Armature|run", 0, 0f); 
      } 
    }//endOfOnCollion 

    void OnCollisionExit(Collision other) { 
     if(other.gameObject.tag == "Ground") { 
     isGrounded = false; 

     } 
     if(!isGrounded) { 

     anim.Play("rig|Character_ActionsJUMP", -1, 0f); 
     anim.Play("Dress_Armature |Jump", 1, 0f); 
     anim.Play("Hair_Armature|Jump", 0, 0f); 
     } 
     if(isGrounded) { 
     anim.Play("rig|Character_Run", -1, 0f); 
     } 
    } 
} 

Antwort

1

Hier ist die Frage:

if(isGrounded) { 

     if(Input.GetKeyDown("space")) { 
      if(isGrounded) 
      { 
      rb.velocity = new Vector3(10, 15, 0); 
      anim.Play("rig|Character_ActionsJUMP", -1, 0f); 
      anim.Play("Dress_Armature |Jump", 1, 0f); 
      anim.Play("Hair_Armature|Jump", 0, 0f); 
      isGrounded = false; 
      } 

      else if (!isGrounded && doubleJump) { 

      rb.velocity = new Vector3(10, 15, 0); 
      anim.Play("rig|Character_ActionsJUMP", -1, 0f); 
      anim.Play("Dress_Armature |Jump", 1, 0f); 
      anim.Play("Hair_Armature|Jump", 0, 0f); 
      doubleJump = false; 

      } 
     } 
     } 

In diesem Code-Block Sie geben nicht die Prüfung für Spacebar gedrückt werden, wenn sie nicht geerdet sind, wenn wir die zweite Prüfung für sie geerdet, Ihren Code ignorieren diese

wird
if(isGrounded) { 

    if(Input.GetKeyDown("space")) { 
     if (!isGrounded && doubleJump) { 

      rb.velocity = new Vector3(10, 15, 0); 
      anim.Play("rig|Character_ActionsJUMP", -1, 0f); 
      anim.Play("Dress_Armature |Jump", 1, 0f); 
      anim.Play("Hair_Armature|Jump", 0, 0f); 
      doubleJump = false; 

      } 
     } 
     } 

Daraus es viel einfacher ist, das Problem zu sehen, wenn kann die letzte nie erfüllt werden, wie geerdet hat beide wahr und fal sein se

Also die Lösung ist Ihre ursprüngliche if-Anweisung zu entfernen, wie es reduant ist und tatsächlich den Rest des Codes bricht!

In diesem Sinne sollte der Code wirklich wie folgt

void Update() { 
    transform.Translate(Vector3.forward * speed * Time.deltaTime); 

if(Input.GetKeyDown("space")) { 
      if(isGrounded) 
      { 
      rb.velocity = new Vector3(10, 15, 0); 
      anim.Play("rig|Character_ActionsJUMP", -1, 0f); 
      anim.Play("Dress_Armature |Jump", 1, 0f); 
      anim.Play("Hair_Armature|Jump", 0, 0f); 
      isGrounded = false; 
      } 

      else if (!isGrounded && doubleJump) { 

      rb.velocity = new Vector3(10, 15, 0); 
      anim.Play("rig|Character_ActionsJUMP", -1, 0f); 
      anim.Play("Dress_Armature |Jump", 1, 0f); 
      anim.Play("Hair_Armature|Jump", 0, 0f); 
      doubleJump = false; 

      } 
     } 
} 
+0

Hallo ich Ihren Code versucht, und es ist nicht dass ich überhaupt springe jetzt! –

+0

natürlich nicht, dieser Code war ein Beispiel, ich kann den tatsächlichen Code bearbeiten, wenn Sie möchten, ich benutzte den Code dort hypothetisch! –

+0

Sorry, ich bin nur ein Anfänger. Wenn Sie den Code bearbeiten könnten, wäre es so hilfreich! Vielen Dank :) –

Verwandte Themen