Unity how to play animation

Unity How to play animation

In Unity, play animation can be achieved in three distinct ways, each offering its unique utility.
Regardless of the method chosen, you will require an Animator component, containing your animation states.

Table of contents

Unity animator tab

How to play Animation Without Parameters

To play an animation in the code, we use the “Play” property in our “Animator“:

using UnityEngine;

public class AnimatorTest : MonoBehaviour
{
    public Animator animator;

    private int idleHash;

    private void Awake()
    {
        idleHash = Animator.StringToHash("Idle");
    }

    public void Start()
    {
        animator.Play(idleHash);
    }
}

This approach is particularly advantageous when you desire full programmatic control over your animations.

The usage of Animator.StringToHash improves performance, as it precomputes a hash of the animation state name.

Although this optimization may not significantly impact on a single call, it becomes essential in scenarios with numerous state changes, such as within an “Update” loop.

Using Bool Parameters

  1. Open the Animator tab.
  2. Create a boolean parameter named “isWalk”
  3. Establish a transition from the “Idle” state to the “Walk” state.
  4. Set the condition “isWalk” to true.

Unity how to play animation boolean

Then, to play the animation, we use the “SetBool” property to set our boolean to true.

using UnityEngine;

public class AnimatorTest : MonoBehaviour
{
    public Animator animator;

    private int _isWalkHash;

    private void Awake()
    {
        _isWalkHash = Animator.StringToHash("isWalk");
    }

    public void Start()
    {
        animator.SetBool(_isWalkHash, true);
    }
}

Employing boolean parameters is particularly beneficial when you wish to initiate an animation when a boolean becomes true and halt it when the boolean becomes false.
This method is ideally suited for animations like character walking cycles.

Utilizing Trigger Parameters

  1. Access the Animator tab.
  2. Generate a trigger parameter named “isWalk”
  3. Define a transition from the “Idle” state to the “Walk” state.
  4. Implement a transition condition based on the trigger “isWalk”

Unity how to play animation trigger

Next, to play the animation with our trigger, this time we use the “SetTrigger” property to activate our trigger.

using UnityEngine;

public class AnimatorTest : MonoBehaviour
{
    public Animator animator;

    private int _isWalkHash;

    private void Awake()
    {
        _isWalkHash = Animator.StringToHash("isWalk");
    }

    public void Start()
    {
        animator.SetTrigger(_isWalkHash);
    }
}

Trigger parameters are ideal for animations such as attack sequences, where you want an animation to play once and automatically exit.

When a trigger is set, it automatically becomes unset upon the animator’s state change.

If you need to reset a trigger for concurrent execution, you can call the “ResetTrigger” function. This method ensures that animations do not get interrupted during execution.

Play animation on keypress

To play an animation when a key is pressed, we can use the Input Manager and its “Input.GetKey” method:

using UnityEngine;

public class AnimatorTest : MonoBehaviour
{
    public Animator animator;

    private int idleHash;

    private void Awake()
    {
        idleHash = Animator.StringToHash("Idle");
    }

    public void Update()
    {
		if (Input.GetKey(KeyCode.A))
		{
			// Key A is held
			animator.Play(idleHash);
		}
        
    }
}

As a reminder, it may now be preferable to use Input System’s new Input Manager instead.

Play animation in reverse

To play the animation in reverse, we can modify our state’s “speed” parameter in the animator and give it a negative value.

Unity play animation reverse

If you’d rather duplicate your animation and turn it into a reverse, I suggest you take a look at this post.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *