Animation gets stuck last frame 2d

Hi everybody i am making a little game to learn the 2d in unity.
I could create a character with various states,his states are “idle” “run” and “punch”.
My problem is when from the idle state i do a punch it gets stuck at the last frame and it doesn’t return in idle,instead when from the run state i give a punch at the end of the punch animation the player’s sprite returns to the run state.
Here’s the part of code that handles the punch.

  void Update()
    {
       if (Input.GetKeyDown(KeyCode.X))
            {
                animator.SetBool("punch", true);
         
    
            }
            
            if (!animator.GetCurrentAnimatorStateInfo(0).IsName("punch") && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))
            {
                animator.SetTrigger("idle");
            }
            else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
            {
                animator.SetTrigger("running");
            }
    }

The parameters of the player’s animator are

  • Trigger “running”
  • Trigger “idle”
  • Bool “punch”

The conditions of the transition from punch to idle are

  • If the Trigger is “idle”
  • if the Bool “punch” is false
  • the exit time is 0.90

What is wrong?

Thanks in advance :stuck_out_tongue:

The order of the ifs should be from biggest priority to lower priority. That means punch will block the access for everything else in that frame, and if you are moving, run and if you are not doing anything, go idle.

Fix:

// If you press X = go punch
if (Input.GetKeyDown(KeyCode.X))
{
    animator.SetBool("punch", true);
} // If you are moving = go run
else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
    animator.SetBool("punch", false);
    animator.SetTrigger("running");  
}
else 
{ // If you are not doing any of the above, go idle
    animator.SetBool("punch", false);
    animator.SetTrigger("idle");
}