JUMP animation does not stop unity2D

Viewed 56

when pressing jump the jump animation wont stop even after landing tried a few thngs but couldnot get it to stop .The run animation works fine but the jumpanimation does not go from jump to idle stays in jump animation and meses everything up new to unity :)

    [SerializeField]
    private float moveforce = 10f;

    [SerializeField]
    private float jumpForce = 11f;

    private float movementX;

    private string RUN_ANIMATION = "run";

    private string JUMP_ANIMATION = "jump";

    private bool isGrounded = true;

    private string GROUND_TAG = "ground";

    private Animator anim;
    
    [SerializeField]
    private Rigidbody2D myBody;

    private SpriteRenderer sr;

    private void Awake()
    {
        sr = GetComponent<SpriteRenderer>();
        anim = GetComponent<Animator>();
        myBody = GetComponent<Rigidbody2D>();
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        PlayerMoveKeyboard();
        AnimatedPlayer();
       
    }

    private void FixedUpdate()
    {
        PlayerJump();
        AnimatedJump();
    }

    void PlayerMoveKeyboard()
    {
        movementX = Input.GetAxisRaw("Horizontal");
        transform.position += new Vector3(movementX, 0f, 0f)  * Time.deltaTime* moveforce;
    }

    void AnimatedPlayer()
    {
        if(movementX > 0)
        {
            anim.SetBool(RUN_ANIMATION, true);
            sr.flipX = false;
        }
        else if(movementX < 0f)
        {
            anim.SetBool(RUN_ANIMATION, true);
            sr.flipX = true;
        }
        else
        {
            anim.SetBool(RUN_ANIMATION, false);
        }
    }

    void PlayerJump()
    {
        if(Input.GetButtonDown("Jump") && isGrounded)
        {
            isGrounded = false;
            myBody.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
           
         }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag(GROUND_TAG))
        {
            isGrounded = true;
        }
    }

    void AnimatedJump()
    {
        if (isGrounded==false)
        {
            anim.SetBool(JUMP_ANIMATION, true);

        }
        else
        {
            anim.SetBool(JUMP_ANIMATION, false);
        }
    }
}
0 Answers
Related