Unity 3D Mecanim Play Animation on Keypress

Unity 3D Mecanim Play Animation on Keypress

by NSDG • Jun 8, 2019 • 0 Comments
308

In this tutorial you'll learn how to Animate a Cube and Play the Animation on keypress using Mecanim.

Unity version used in this tutorial: Unity 2018.3.0f2 (64-bit)

Steps

  • Create new Cube (GameObject -> 3D Object -> Cube)

  • Select Cube then open Animation window (Window -> Animation -> Animation)
  • In Animation window click "Create", which will create a new animation, new Animator Controller and will attach an Animator component to the Cube.
  • Name the new animation "SpinAnimation"

Tip: To create more animations, simply click on the animation name then click "Create new Clip..."

  • In the Animation Window Click AddProperty -> Transform -> Rotation
  • This animation will have 2 frames, start and finish.
  • For the first frame we set rotation to (0, 0, 0)

  • For the last frame we set rotation to (0, 360, 0)

The animation is ready.

  • Now Double click on the Animator Controller that was created with the animation

  • New Window titled "Animator" will appear, which should look something like this:

You will notice there is an arrow pointing from the "Entry" to the "SpinAnimation", which means that the animation will be played as soon as the Scene starts, but that's not what we want, we want to only play the animation on key press.

Since Mecanim requires to have an entry animation, what we can do is create a new Empty state and assign it as Entry:

  • Right Click on Animator window -> Create State -> Empty:

  • Select Entry state -> Right click on it -> Set StateMachine Default State, then point to "New State":

The last thing left to do is to make a transition to SpinAnimation, but we only want to do the transition once the specific condition is met, that's when Parameters Tab comes in place:

  • Click Parameters tab in Animator window then click (+) symbol:

You will notice there is multiple types available, but for now we will select Trigger, it's similar to bool but unlike bool it's automatically sets itself to false once the animation is completed, useful for one shot animations:

  • Name the parameter "spinTrigger"

  • Lastly select "Any State" -> Right Click on it -> Make Transition, then point to SpinAnimation:

  • Click on the transition Arrow and in Inspector View under Conditions click (+) and make sure spinTrigger parameter is selected

  • By default animations created with Unity have loop enabled, but since we only want to play Animation once, what we need to do is select the Animation Clip in the Project view then uncheck the Loop Time checkbox:

Finally we need to create a script which will trigger the parameter on key press. Check the code below:

AnimationTest.cs

using UnityEngine;

public class AnimationTest : MonoBehaviour
{

    Animator animator;

    // Start is called before the first frame update
    void Start()
    {
        //Get Animator component
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        //Play spin animation on key press
        if (Input.GetKeyDown(KeyCode.Q))
        {
            animator.SetTrigger("spinTrigger");
        }
    }
}
  • Attach AnimationTest.cs script to Cube:

Now let's test it:

Everything works as expected, pressing Q plays the animation.