# How to make an animation play when a button is pressed once?

I have added an idle and jump animation to a character in unity. I can make the animation happen when the button is pressed. If the button is up middle at the animation the character goes to the idle position. But I want the whole animation happen in one press to the button and then go back to the idle position. My current script and Animator views are:

if (Input.GetKeyDown(KeyCode.Space))
{
Anim.SetBool("is_jumping", true);
ApplyInput(moveAxis, turnAxis);
}
else if (Input.GetKeyUp(KeyCode.Space))
{
Anim.SetBool("is_jumping", false);
}


Use a Trigger instead of a Boolean for this.

A trigger is like a boolean, except that it automatically resets itself to false after one frame if you set it to true.

if (Input.GetKeyDown(KeyCode.Space))
{
Anim.SetTrigger("is_jumping");
ApplyInput(moveAxis, turnAxis);
}


Note that you will need to replace "is_jumping" with a trigger in the animator.

• it does not go back to idle position. How can i get it back to idle position after the jump animation ends? Oct 4, 2019 at 16:57
• The transition to the idle animation from the jump animation should have "Exit time" checked. This will make the jump animation automatically transition to the idle animation after it ends. Oct 4, 2019 at 16:58