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 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 animation events and state machine behaviors for optimizing animator workflows.

Table of contents

Animation Event: Basic Usage

Event animation on Unity is made possible by a GameObject with an attached “Animator” component and at least one animation clip required. We will consider an example where we have a “Death” animation. Our objective is 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.

Animation Event with Parameters

In Unity, animation events support parameters fore more 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 an individual function for each unique message.

StateMachineBehaviour

Working with the Unity Animator system often involves detecting when a state transition will occur. For example, when transitioning between states using a Boolean condition, the accuracy of a change of state is not guaranteed. StateMachineBehaviours addresses this issue and offers 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 inherit from StateMachineBehaviour and contain overridden functions for various events (OnStateEnter, OnStateExit, OnStateIK, OnStateMove, OnStateUpdate).

Unity StateMachineBehaviour

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. With StateMachineBehaviours, actions can be directed to a MonoBehaviour, enhancing flexibility in managing animations. Caching the MonoBehaviour instance for performance reasons is also demonstrated.

In a scenario with numerous animators, performance optimization is possible using the SharedBetweenAnimators attribute. In this case, 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.

Leave a Reply

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