My character switches back to Idle mode for a second while jumping.

Hey, I’m pretty new to game development and I was trying to make my character jump, but whenever it jumps, it returns to idle mode for a short period of time and only then will it go to falling mode. Is there something wrong with the code or Animator setup? Thank you in advance!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Character : MonoBehaviour
{
    private Rigidbody2D rb;
    private Animator anim;
    private float moveSpeed;
    private float dirX;
    private bool facingRight = true;
    private Vector3 localScale;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        localScale = transform.localScale;
        moveSpeed = 5f;
    }

    private void Update()
    {
        dirX = Input.GetAxisRaw("Horizontal") * moveSpeed;

        if (Input.GetButtonDown("Jump") && rb.velocity.y == 0)
            rb.AddForce(Vector2.up * 700f);

        if (Mathf.Abs(dirX) > 0 && rb.velocity.y == 0)
            anim.SetBool("isRunning", true);
        else
            anim.SetBool("isRunning", false);

        if (rb.velocity.y == 0)
        {
            anim.SetBool("isJumping", false);
            anim.SetBool("isFalling", false);
        }

        if (rb.velocity.y > 0)
            anim.SetBool("isJumping", true);

        if (rb.velocity.y < 0)
        {
            anim.SetBool("isJumping", false);
            anim.SetBool("isFalling", true);
        }
    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector2(dirX, rb.velocity.y);
    }

    private void LateUpdate()
    {
        if (dirX > 0)
            facingRight = true;
        else if (dirX < 0)
            facingRight = false;

        if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
            localScale.x *= -1;

        transform.localScale = localScale;
    }
}

Hey!

Your description is a little hard to understand. Can you please confirm -

  • Is your character standing or running when this happens?
  • Is the JumpUp animation working?

I can see you don’t have a transition from Run to JumpUp, and you don’t have an animation from JumpDown to Idle.

As a tip - While testing your animations, make sure to select your player and look at the parameters in the Animator window to make sure they are working as you’d expect. You may find a parameter is not behaving the way you expect.

Once I have a better understanding, I’ll see if I can figure it out!