0
\$\begingroup\$

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

enter image description here

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ it does not go back to idle position. How can i get it back to idle position after the jump animation ends? \$\endgroup\$ – Jacob Frye Oct 4 '19 at 16:57
  • \$\begingroup\$ 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. \$\endgroup\$ – Koala Squad Oct 4 '19 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.