Version: 2022.3
Language : English
GameObjects with Multiple Moving Parts
Animator Controllers

Use Animation Events

Use an Animation EventAllows you to add data to an imported clip which determines when certain actions should occur in time with the animation. For example, for an animated character you might want to add events to walk and run cycles to indicate when the footstep sounds should play. More info
See in Glossary
to call a function at a specific point in time. This function can be in any script attached to the GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary
but must only accept a single parameter of type float, int, string, an object reference, or an AnimationEvent object.

// This C# function can be called by an Animation Event
public void PrintFloat (float theValue) {
    Debug.Log ("PrintFloat is called with a value of " + theValue);
}

To add an Animation event to a clip at the current playhead position, click the Event button. To add an Animation event at any position, right-click the Event line where you want to add the Event and select Add Animation Event from the context menu. Once added, click and drag the Animation event to reposition it on the Event Line.

Animation Events display in the Event Line
Animation Events display in the Event Line

When you add an Event, the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary
Window displays the Function field. Use this field to select the method you want to call. Note that Animation Events only support methods with a single parameter. You cannot select a function that accepts more than one parameter.

However, you can use an AnimationEvent object to pass many parameters at the same time. An AnimationEvent object accepts a float, an int, a string, and an object reference as member values. The AnimationEvent object also provides information about the Animation Event that calls the function.

The Inspector Window with an Animation Event selected. The PrintString method is selected from ExampleClass.
The Inspector Window with an Animation Event selected. The PrintString method is selected from ExampleClass.

The Events added to a clip are shown as markers in the Event line. Hover the cursor over a marker to display a tooltip with the function name and parameter value.

You can select and manipulate multiple Events in the Event Line. To select multiple Events in the Event Line, hold the Shift key and click each Event marker one by one. To remove a marker from the selection, hold Shift and click a selected marker.

You can also use a selection box to select multiple Animation Events. To do this, click and drag within the Event Line:

To delete an Animation Event, select it and press the Delete key. You can also right-click the Animation Event and choose Delete Event from the context menu.

Example

This example demonstrates how to add Animation Events to a simple GameObject. When all the steps are followed, the Cube animates forwards and backwards along the x-axis during Play mode, and the Event message displays in the console every second, at the 0.8 second time.

This example requires a small script with the function PrintEvent(). This function prints a debug message which includes a string (“called at:”) and the time:

// This C# function can be called by an Animation Event
using UnityEngine;
using System.Collections;


public class ExampleClass : MonoBehaviour
{
    public void PrintEvent(string s)
    {
        Debug.Log("PrintEvent: " + s + " called at: " + Time.time);
    }
}

Create a script file with this example code and place it in your Project folder (right-click inside the Project window in Unity and select Create > C# Script, then copy and paste the above code example into the file and save it).

In Unity, create a Cube GameObject (menu: GameObject > 3D Object > Cube). To add your new script file to it, drag and drop it from the Project window into the Inspector window.

Select the Cube and then open the Animation window (menu: Window > Animation > Animation or press ctrl+6). Set a Position curve for the x coordinate.

Animation window
Animation window

Next, set the animation for the x coordinate to increase to around 0.4 and then back to zero over 1 second, then create an Animation Event at approximately 0.8 seconds. Press Play to run the animation.

GameObjects with Multiple Moving Parts
Animator Controllers