My character returns to idle mode for a second.

Hey, I’m new to game development and I have a question: How to make my character stop going to idle mode mid-air?
Whenever my character is jumping, it goes to idle mode for less then a second. It might be short but it makes the animation looks weird and I don’t know how to fix it…
Below the code and animator:

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;
    }
}

Not particularly sure, as someone should have a much better answer. But I believe the problem to be your checks within velocity being zero. Therefor while at the precipice of your jump, the character may think that velocity is zero, now become idle.

So my suggestion would be to set all this up with enums, instead of a bunch of bools. As enums basically are a list of bools, but only allow one to be active at a time, and turn the others false when one is made true. Example:

public enum CharacterStates // or name it whatever
{
     Idle,
     Running,
     Jumping,
     Falling,
     Shooting,
     Dying
}
public CharacterStates state;

Therefore only specific code will run while said state is active. So then you can specifically make the way, or transition, from Jumping to then turn on Falling, to be that of velocity = zero. And obviously find a way to give a bit of time, or a bool check to make sure hitGround or something like that, before any allowance is given for Falling state to then allow transition into Running, or Idle. :slight_smile:

if (jump) { state = CharacterState.Jumping; }

if (state == CharacterState.Jumping)
{ if (velocity == 0) { state = CharacterState.Falling;} }

// tip: you may wish to have a shorter name then CharacterState, your     
// fingers will thank you later

Hey @wideeyenow_unity , thanks for answering, I will try it.