Trigger an animation with a key press
Trigger an animation from the Animation Director when pressing a specific key.
Read time 1 minuteLast updated 2 days ago
Use this example to understand how to trigger an animation from the Animation Director when the user presses a specific key.
Create an Animation with the Animation Director.
- Use the Animation Director to create an animation and give it a name. For more information, refer to Animate multiple GameObjects with the Animation Director.
Create a New Logic script.
- In the Project panel, select the My Logic folder.
- Select Add (+).
- Select New Script. Unity Lite creates a new Logic script in the folder.
- Enter a name for your script and press Enter.
Add the script to a GameObject.
- Select or create a GameObject from the Hierarchy panel. The Inspector panel shows the GameObject's properties.
- In the Inspector panel, select Add Component.
- Choose your Logic script from the list.
Set up the logic in the Logic Editor.
- In the Project panel, double-click the Logic script to open the Logic Editor.
- Drag a When event listener into the Logic Editor.
- Drag a Key Pressed event node into the When node.
- Set the key you want to use to trigger the animation. For example, the Space key.
- Create a new variable named and set its type to Animation Director.
Animation Director
- Expand the Animation Director's properties and methods, then drag the Play method into your logic chain.
- Type the name of the animation you want to play in the socket.

Example Logic script to trigger an animation on key press.
- Close the Logic Editor.
Connect your Animation Director reference.
- In the Hierarchy panel, select the GameObject that has your Logic script attached.
- Drag the Animation Director from the Hierarchy panel into the Animation Director variable reference in your script component.