Address
304 North Cardinal St.
Dorchester Center, MA 02124

Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM

Unity animation event add event

Unity Animation Event

In Unity, the capacity to trigger events at specific moments within an animation is an invaluable tool for game developers. Unity provides a feature called “animation events” that allows for the seamless integration of custom behaviors or actions at precise timestamps during an animation sequence.

However, when managing multiple events within a single state, Unity offers a more advanced solution known as StateMachineBehaviour. This article explores the use of animation events and the integration of state machine behaviors for optimizing animator workflows.

Table of contents

Unity Animation Event: Basic Usage

To utilize animation events in Unity, a GameObject with an attached “Animator” component and at least one animation clip is required. Let’s consider an example where we have an “Death” animation, and we want to trigger an event when the character begins to kneel.


using UnityEngine;

public class AnimatorTest : MonoBehaviour
{
    // We will trigger this event
    public void BeginFallen()
    {
        // Custom actions can be placed here
    }
}

  1. Select the GameObject within the scene hierarchy.
  2. Add the “AnimatorTest” component.
  3. Open the Animation tab via “Window” -> “Animation” -> “Animation.”
  4. Choose the specific animation clip.
  5. Use the animation timeline slider to navigate to the desired event time.
  6. Click the “Add Event” button to add an animation event at the selected time.

Unity animation event add event

In the Inspector, you can now associate the animation event with the designated function (e.g., “BeginFallen”).

Unity animation event add function

It’s essential to note that any other animator using this animation should also include the “AnimatorTest” component to avoid exceptions.

Unity Animation Event with Parameters

In Unity, animation events support parameters, which can enhance flexibility and reusability. For instance, consider a scenario where you need to display various messages on the screen at specific animation times. Instead of creating separate methods for each message, you can use a parameterized approach:


public void UIMessage(string message)
{
    UIManager.Instance.DisplayMessage(message);
}

Unity animation event with parameter

With this approach, you can send any message as a parameter to the “UIMessage” function, eliminating the need to create individual functions for each unique message.

Unity StateMachineBehaviour

Working with Unity’s Animator system often involves the need to know when a state transition will occur. For example, when transitioning between states using a Boolean condition, there’s no guarantee that the state change will take place immediately. StateMachineBehaviours address this issue and offer additional functionality.

In the Animator tab (“Window” -> “Animation” -> “Animator”), you can add a StateMachineBehaviour to a specific state or to all states within a layer. These behaviors are scripts that inherit from StateMachineBehaviour and contain overridden functions for various events (OnStateEnter, OnStateExit, OnStateIK, OnStateMove, OnStateUpdate).

Unity StateMachineBehaviour

Here’s an example that uses OnStateEnter and OnStateExit:


using UnityEngine;

public class EventAnimation2nd : StateMachineBehaviour
{
    private AnimatorTest _animatorTest;

    public AnimatorTest GetAnimatorTest(Animator animator)
    {
        if (_animatorTest == null)
        {
            _animatorTest = animator.GetComponent();
        }

        return _animatorTest;
    }

    // OnStateEnter is called when a transition starts and the state machine begins evaluating this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        var animatorTest = GetAnimatorTest(animator);
        // Custom actions can be called within the StateMachineBehaviour
    }

    // OnStateExit is called when a transition ends, and the state machine completes the evaluation of this state
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        var animatorTest = GetAnimatorTest(animator);
        // More custom actions can be executed upon state exit
    }
}

In the provided code, “AnimatorTest” represents a MonoBehaviour attached to the GameObject. By using StateMachineBehaviours, actions can be directed to a MonoBehaviour, enhancing flexibility in managing animations. Caching the MonoBehaviour instance for performance reasons is also demonstrated.

For scenarios with numerous animators, performance can be optimized using the SharedBetweenAnimators attribute. This ensures that the StateMachineBehaviour is instantiated only once and shared among multiple animators. When using this attribute, a slight modification of the cache code will be necessary for compatibility.

By implementing animation events and StateMachineBehaviours, Unity developers can significantly enhance their control over animations and streamline complex workflows.

Have you just started your Unity adventure?
Check this article to start in the best possible way: The Best Advice I Wish I Had When I Started Game Development

Check our other articles about Unity:

Leave a Reply

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