0
0
UnityHow-ToBeginner ยท 4 min read

How to Use Animation Events in Unity: Simple Guide

In Unity, use Animation Events to call functions at specific times during an animation clip. Add events in the Animation window by selecting a frame and assigning a function name from your script. The function must be public and match the event signature to be triggered correctly.
๐Ÿ“

Syntax

An Animation Event calls a method in your script at a specific time during an animation clip. The method must be public and can have zero or one parameter of type float, int, string, or Object.

Example method signatures:

  • public void MyFunction()
  • public void MyFunction(float value)

In the Animation window, you add an event by clicking the timeline and entering the method name exactly.

csharp
public class AnimationEventHandler : MonoBehaviour
{
    public void OnAnimationEvent()
    {
        Debug.Log("Animation event triggered!");
    }

    public void OnAnimationEventWithFloat(float value)
    {
        Debug.Log($"Animation event with value: {value}");
    }
}
๐Ÿ’ป

Example

This example shows how to create a script with a method triggered by an animation event. Add the event in the Animation window at the desired frame and type the method name PlaySound. When the animation reaches that frame, the sound plays.

csharp
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    public AudioSource audioSource;

    public void PlaySound()
    {
        if (audioSource != null)
        {
            audioSource.Play();
            Debug.Log("Sound played via animation event.");
        }
    }
}
Output
Sound played via animation event.
โš ๏ธ

Common Pitfalls

  • Method name mismatch: The method name in the event must exactly match the script method name, including case.
  • Method visibility: The method must be public to be called by the animation event.
  • Parameter mismatch: If the event sends a parameter, the method must accept the correct type and number of parameters.
  • Script not attached: The script with the method must be attached to the same GameObject as the Animator.
csharp
/* Wrong: private method - will not be called */
private void EventMethod()
{
    Debug.Log("Won't be called");
}

/* Right: public method */
public void EventMethod()
{
    Debug.Log("Called correctly");
}
๐Ÿ“Š

Quick Reference

StepDescription
1. Open Animation WindowWindow > Animation > Animation
2. Select Animation ClipChoose the clip to add events to
3. Add EventClick timeline at desired frame, press Add Event button
4. Enter Method NameType the exact public method name from your script
5. Attach ScriptEnsure script with method is on the same GameObject
6. TestPlay animation and watch for event-triggered behavior
โœ…

Key Takeaways

Animation events call public methods at specific animation frames.
Method names must match exactly and be public to work.
Attach the script with event methods to the same GameObject as the Animator.
Animation events can pass one parameter of type float, int, string, or Object.
Use the Animation window to add and configure events visually.