2016-10-14 6 views
1

In meinem 2D Unity-Projekt springt mein Player nicht immer, während er die Sprungtaste drückt. Er springt nicht in der Sekunde, in der ich auf dem Boden lande, aber nach einer Sekunde, in der er "geerdet" ist, kann er wieder springen. Was kann dieses Problem sein?Mein Player springt nicht immer, wenn ich die Sprungtaste drücke

using UnityEngine; 
    using System.Collections; 
    using UnityEngine.UI; 

    public class player : MonoBehaviour { 

     private static player instance; 

     public static player Instance 
     { 
      get 
      { 
       if (instance == null) 
       { 
        instance = GameObject.FindObjectOfType<player>(); 
       } 
       return instance; 
      } 

     } 


     private Animator myAnimator; 

     [SerializeField] 
     public static float movementSpeed; 

     private bool facingRight = true; 

     [SerializeField] 
     private Transform[] groundPoints; 

     [SerializeField] 
     private float groundRadius; 

     [SerializeField] 
     private LayerMask whatIsGround; 

     [SerializeField] 
     private bool airControl; 

     [SerializeField] 
     private float jumpForce; 

     public bool canMove; 

     public AudioClip jump001; 
     public AudioClip jump002; 

     private float direction; 
     private bool move; 
     private float btnHorizontal; 

     public Rigidbody2D MyRigidbody { get; set; } 

     public bool Attack { get; set; } 

     public bool Jump { get; set; } 

     public bool OnGround { get; set; } 





     // Use this for initialization 
     void Start() { 

      facingRight = true; 
      MyRigidbody = GetComponent<Rigidbody2D>(); 
      myAnimator = GetComponent<Animator>(); 




     } 
     void Update() 
     { 

      HandleInput(); 

     } 
     // Update is called once per frame 
     void FixedUpdate() 
     { 

      OnGround = IsGrounded(); 

      float horizontal = Input.GetAxis("Horizontal"); 

      if (move) 
      { 
       this.btnHorizontal = Mathf.Lerp(btnHorizontal, direction, Time.deltaTime * 5); 
       HandleMovement(btnHorizontal); 
       Flip(direction); 
      } 
      else 
      { 

       HandleMovement(horizontal); 

       Flip(horizontal); 
      } 

      if (!canMove) 
      { 
       GetComponent<Rigidbody2D>().velocity = new Vector2(0, GetComponent<Rigidbody2D>().velocity.y); 
       myAnimator.SetFloat("speed", 0); 
       return; 
      } 




      HandleLayers(); 
     } 


     private void HandleMovement(float horizontal) 
     { 
      if (MyRigidbody.velocity.y < 0) 
      { 
       myAnimator.SetBool("land", true); 
      } 
      if (!Attack && (OnGround || airControl)) 
      { 
       MyRigidbody.velocity = new Vector2(horizontal * movementSpeed, MyRigidbody.velocity.y); 
      } 
      if (Jump && MyRigidbody.velocity.y == 0) 
      { 
       SoundManager.instance.RandomizeSfx(jump001, jump002); 
       MyRigidbody.AddForce(new Vector2(0, jumpForce)); 
      } 

      myAnimator.SetFloat("speed", Mathf.Abs(horizontal)); 
     } 


     private void HandleInput() 
     { 
      if (canMove) 
      { 

       //Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W) || 
       if (Input.GetButtonDown("Jump")) 
       { 
        myAnimator.SetTrigger("jump"); 

       } 
       if (Input.GetKeyDown(KeyCode.Z) || Input.GetButton("Fight") && OnGround && !Jump) 
       { 
        myAnimator.SetTrigger("attack"); 
       } 
      } 
     } 

     private void Flip(float horizontal) 
     { 
      if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight && canMove) 
      { 
       facingRight = !facingRight; 

       Vector3 theScale = transform.localScale; 

       theScale.x *= -1; 

       transform.localScale = theScale; 
      } 

     } 


     private bool IsGrounded() 
     { 
      { 

      } 
      if (MyRigidbody.velocity.y <= 0) 
      { 
       foreach (Transform point in groundPoints) 
       { 
        Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround); 

        for (int i = 0; i < colliders.Length; i++) 
        { 
         if (colliders[i].gameObject != gameObject) 
         { 
          return true; 
         } 
        } 
       } 
      } 
      return false; 
     } 

     private void HandleLayers() 
     { 
      if (!OnGround) 
      { 
       myAnimator.SetLayerWeight(1, 1); 
      } 
      else 
      { 

       myAnimator.SetLayerWeight(1, 0); 
      } 

     } 

     //TouchKnappar 

     public void BtnJump() 
     { 
      if (canMove) 
      { 


       myAnimator.SetTrigger("jump"); 
       Jump = true; 
      } 
     } 
     public void BtnAttack() 
     { 

       myAnimator.SetTrigger("attack"); 
       Attack = true; 

     } 
     public void BtnMove(float direction) 
     { 

       this.direction = direction; 
       this.move = true; 

     } 
     public void BtnStopMove() 
     { 

       this.direction = 0; 
       this.btnHorizontal = 0; 
       this.move = false; 

     } 
     public void BtnStopJump() 
     { 

       Jump = false; 


    } 


    } 
+0

Ist es möglich, dass dein Charakter ein wenig kurz springt, wenn er landet, was dazu führt, dass der Check 'MyRigidbody.velocity.y <= 0' fehlschlägt? Versuchen Sie es mit 'Debug.Log()', um dies zu überprüfen. – Serlite

+0

Ich vermute, dass die velocity.y ein wenig von 0 auf dem Boden ist und dauert ein paar Sekunden, um genau 0 zu werden. Und die velocity.y könnte sogar für ein bisschen negativ sein. Es könnte also funktionieren (Jump && MyRigidbody.velocity.y == 0) zu (Jump && MyRigidbody.velocity.y> = 0). Aber, ehrlich gesagt, sollte der ganze Code überarbeitet werden ... – Alox

+0

Okay danke, ich werde dazu schauen. Dies ist mein erstes Spiel und ich folgte einem Typen auf Youtube und habe selbst einige Änderungen vorgenommen. Ich verstehe den ganzen Code nicht vollständig. –

Antwort

1

Ich hatte das gleiche Problem und das reparierte es für mich.

Dieser Code identifiziert, wenn das Zeichen in FixedUpdate geerdet ist()

Collider2D groundCol = Physics2D.OverlapBox(groundCheck.position, groundBoxRadius, 0f, whatIsGround); 
this.grounded = (groundCol != null && !groundCol.isTrigger && this.rigbody2d.velocity.y > -0.01f && this.rigbody2d.velocity.y < 0.01f); 

Einige Erklärung:

  • Erdungs- ist ein leeres Objekt an den Füßen des Charakters
  • Im Zustand I überprüfe, ob die Overlay-Box mit etwas kollidiert ist und ob das etwas nicht auslöst
  • Schließlich kann die Geschwindigkeitsgeschwindigkeit manchmal nein sein t genau ein 0 so + -0,01f hat für mich funktioniert
Verwandte Themen