Animation changing for split second

I have a falling and jumping animation for my character and whenever my character reaches the peak of his jump his animation changes to the idle animation for a split second how do i fix this? this is my code:

public class PlayerScript : MonoBehaviour
{
    private Rigidbody2D rb;
    private SpriteRenderer sprite;
    private Animator anim;

    private float dirX = 0f;
    [SerializeField]private float moveSpeed = 7f;
    [SerializeField]private float jumpForce = 14f;
    private enum movementState {idle, running, jump, fall }

    // Start is called before the first frame update
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        sprite = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    private void Update()
    {
        dirX = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);

        if (Input.GetButtonDown("Jump"))
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }

        UpdateAnimateState();
    }

    private void UpdateAnimateState()
    {
        movementState state;

        if (dirX > 0f)
        {
            state = movementState.running;
        }
        else if (dirX < 0f)
        {
            state = movementState.running;
        }  
        else
        {
            state = movementState.idle;
        }

        if (rb.velocity.y > .1f)
        {
            state = movementState.jump;
        }
        else if (rb.velocity.y < -.1f)
        {
            state = movementState.fall;
        }

        anim.SetInteger("State", (int)state);

    }


  
}

Also if you are moving while jumping it starts trying to play the walk animation for a split second

In your animator settings try playing with exit time and transition time and see if it helps

        if (rb.velocity.y > .1f)
        {
            state = movementState.jump;
        }
        else if (rb.velocity.y < -.1f)
        {
            state = movementState.fall;
        }

I think this is your problem, when your when your y velocity is between 0.1 and -0.1 which is at the top. Your conditions for jump states doesn’t met. So your state becomes as idle or running.

how do i fix that?

Checking velocity of y is not a good way to find out if character is jumping. I haven’t done something similar yet but I often saw an “IsGrounded” bool used for this. I’m not sure about the condition used for it, but I’m sure you can find something about this if you check the net.

and how do i use the isgrounded bool in my code

wherever I try to put the bool l it doesnt work

this is my code am i supposed to use an else if or just an if statement

  • public class PlayerScript : MonoBehaviour
  • {
  • private Rigidbody2D rb;
  • private SpriteRenderer sprite;
  • private Animator anim;
    • private float dirX = 0f;
  • [SerializeField]private float moveSpeed = 7f;
  • [SerializeField]private float jumpForce = 14f;
  • private enum movementState {idle, running, jump, fall }
    • // Start is called before the first frame update
  • private void Start()
  • {
  • rb = GetComponent();
  • anim = GetComponent();
  • sprite = GetComponent();
  • }
    • // Update is called once per frame
  • private void Update()
  • {
  • dirX = Input.GetAxisRaw(“Horizontal”);
  • rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
    • if (Input.GetButtonDown(“Jump”))
  • {
  • rb.velocity = new Vector2(rb.velocity.x, jumpForce);
  • }
    • UpdateAnimateState();
  • }
    • private void UpdateAnimateState()
  • {
  • movementState state;
    • if (dirX > 0f)
  • {
  • state = movementState.running;
  • }
  • else if (dirX < 0f)
  • {
  • state = movementState.running;
  • }
  • else
  • {
  • state = movementState.idle;
  • }
    • if (rb.velocity.y > .1f)
  • {
  • state = movementState.jump;
  • }
  • else if (rb.velocity.y < -.1f)
  • {
  • state = movementState.fall;
  • }
    • anim.SetInteger(“State”, (int)state);
    • }
        • }

I think you are using this tutorial, I’m suprised that his code doesn’t have same problem. You can go with a better and more complicated solution such as checking collision between player and terrain to see it player is grounded (I think he does this in the following videos). But in this case changing animator state transitions would probably help.

Kill connection from jumping to running, jumping to idle, and faling to jumping. If you not gonna double jump this should be fine. In that case add faling to jumping connection back.

9000994--1240078--Untitled.jpg

This helped so much thank!:slight_smile: