Documentation

​
​

Development

User Acquisition

Monetization

Industry

Unity Studio

Open Unity Studio

Unity Studio

​
​
Unity Studio
  • What's new in Unity Studio
  • Introduction to Unity Studio
  • Launch Unity Studio
  • Create your first Unity Studio project
  • Develop with Unity Studio
    • The Unity Studio interface
    • Save your progress
    • GameObjects
    • Assets
    • Materials
    • Scene settings
    • Logic
      • Introduction to Logic
      • Create a Logic script
      • The Logic Editor interface
      • Methods, variables, and parameters
      • Nodes
      • Logic examples
        • Use a button to transform a GameObject
        • Hide or show a User Interface (UI) element
        • Trigger an animation on key press
        • Adjust scene lighting with a button
        • Interact with an asset after an event
        • Display the name of a GameObject on hover
        • Enable or disable a property on a GameObject
        • Play audio when you click an object
        • Mute and unmute audio with the toggle element
        • Use sliders to change a material's color
    • The UI Creator
    • Animation
    • AI features
  • Collaborate in Unity Studio
  • Export drafts to the Unity Editor
  • Publish and share
  • Troubleshooting Unity Studio issues
  • Report bugs and share feedback
  • Manage your analytics and marketing preferences
  1. Unity Studio
  2. Develop with Unity Studio
  3. Logic
  4. Logic examples

Trigger an animation with a key press

Trigger an animation from the Animation Director when pressing a specific key.
Read time 2 minutes
Last updated 2 months ago

Use this example to understand how to trigger an animation from the Animation Director when the user presses a specific key.
1

Create an Animation with the Animation Director

  1. 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.
2

Create a New Logic script

  1. In the Project panel, select the My Logic folder.
  2. Select Add (+).
  3. Select Script. Unity Studio creates a new Logic script in the folder.
  4. Enter a name for your script and press Enter.
3

Add the script to a GameObject

  1. Select or create a GameObject from the Hierarchy panel. The Inspector panel shows the GameObject's properties.
  2. In the Inspector panel, select Add Component.
  3. Choose your Logic script from the list.
4

Set up the logic in the Logic Editor

  1. In the Project panel, double-click the Logic script to open the Logic Editor.
  2. Drag a When event listener into the Logic Editor.
  3. Drag a Key Pressed event node into the When node.
  4. Set the key you want to use to trigger the animation. For example, the Space key.
  5. Create a new variable named
    Animation Director
    and set its type to Animation Director.
  6. Expand the Animation Director's properties and methods, then drag the Play method into your logic chain.
  7. Type the name of the animation you want to play in the socket.
Example Logic script to trigger an animation on key press.

Example Logic script to trigger an animation on key press.

  1. Close the Logic Editor.
5

Connect your Animation Director reference

  1. In the Hierarchy panel, select the GameObject that has your Logic script attached.
  2. Drag the Animation Director from the Hierarchy panel into the Animation Director variable reference in your script component.
Press Play to test your scene. The animation plays when the user presses the specified key.
Tip
Use the same approach to pause or stop an animation. If you use a Stop method, it stops all animations from the Animation Director. To stop only one specific animation, use the Stop Specific method and type the animation’s name in the socket.

Additional resources

  • Introduction to Logic
  • The Logic Editor interface
  • Create a Logic script
  • Methods and variables
  • Nodes
  • Logic examples

Copyright © 2026 Unity Technologies
LegalPrivacy PolicyCookiesDocumentation Terms of UseDo Not Sell or Share My Personal InformationYour Privacy Choices (Cookie Settings)

"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S and elsewhere (more info here). Other names or brands are trademarks of their respective owners.

Some pages are machine-translated for convenience, and may contain inaccuracies. In the event of conflicting information, the English version is authoritative.

  • On this page
    • Additional resources


Report a problem with this page