In this tutorial, we will explore how to use the Animator in Unity to create and manage animations for a 3D object. Animator Controllers are an essential part of Unity for animating characters and other objects in your game, and by the end of this guide, you’ll be comfortable using them to animate a simple 3D Cube.

What is an Animator in Unity?
An Animator Controller is a tool in Unity that allows you to arrange, manage, and control a set of animations for a character or any animated GameObject. It uses a State Machine, which can be thought of as a flowchart or a simple program that controls the transitions between various animation states.
Read Now : The Best Game Developer Online
Key Features of Animator Controller:
- State Machine: Manages the different states your animations can be in, and handles transitions between them.
- Animation Clips: Holds the animation data that is applied to objects.
- Visual Scripting: Provides a visual interface for creating animation logic, eliminating the need for manual coding.
Step-by-Step Tutorial: Creating Animations with Animator in Unity
Step 1: Create a New Scene
To begin, open Unity and create a new scene where you’ll be working on your animations.
- Open Unity.
- Select File > New Scene from the menu bar.
Step 2: Create a 3D Cube Game Object
Next, we’ll create a 3D Cube to animate.
- In the Hierarchy window, right-click and choose 3D Object > Cube to create a new cube in the scene.
- Position your camera so that it’s facing the cube. You can do this by selecting the Camera object and adjusting its position and rotation values until the cube is visible.
Step 3: Open the Animation Window
Now, let’s open the Animation window, which is where we will create and manage our animations.
- Go to the Window menu in the Unity editor.
- Select Animation to open the Animation window.
Step 4: Create a New Animation Clip
We’ll start by creating an animation for our cube.
- In the Animation window, click on the Create button located in the middle of the window.
- A file dialog will appear asking you to specify a name for your new animation. Name it something like RedAnim and save it in your project folder.
Step 5: Add Property to Animation Clip
Next, we’ll add a property to the animation that we can animate. We’re going to change the cube’s color.
- In the Animation window, click the Add Property button.
- In the property list, navigate to Mesh Renderer > Materials > _Color.
- Once you’ve selected _Color, click the + icon to add it to the list of properties for the animation.
This property will control the color of the cube in the animation.
Step 6: Create the Base Animation
Now that we’ve added the color property, we need to make the cube white by default for the base animation.
- In the Animation window, you’ll see a timeline. By default, the _Color property will have a keyframe at time 0.
- Click on the color field and set it to white (#FFFFFF).
- The cube will now be white in the base animation.
Step 7: Create Another Animation for Red Color
Now, we need to create another animation where the cube will turn red.
- Click the RedAnim header in the Animation window.
- Choose Create New Clip… from the dropdown menu.
- In the file dialog that appears, name the new animation (e.g., RedColorAnim), and save it.
- In this new animation clip, select the _Color property and change the color to red (#FF0000).
- Adjust the timeline and set keyframes for smooth transition if necessary.
Step 8: Add Another Animation for Transition Back to White
To make the transition back to white more natural, we’ll create another animation that smoothly changes the cube’s color from red to white.
- Another dialog box will pop up asking you to specify what to save the file as. Name it Base.
- Just like in the previous steps, click Add Property, then choose Mesh Renderer → Material._Color again to add the color property.
- Make sure the timeline is set at 0:00 and change the green (g) and blue (b) values of the color to 0 (making the color pure red at the start).
- Now, at a later point in the timeline, change the color back to white (#FFFFFF), making the transition look more natural.
Step 9: Create the Animator Controller
Now, we need to create an Animator Controller to manage the transitions between our animations.
- In the Project window, right-click and select Create > Animator Controller.
- Name the new controller file (you can name it something like CubeAnimator).
Step 10: Set Up the Animator Controller
Now that we have our Animator Controller, let’s set it up to control the animations.
- Double-click on the Animator Controller to open the Animator window (Animator Controller editor).
- Drag and drop the three animation clips (i.e., RedAnim, RedColorAnim, and Base) into the Animator window.
Step 11: Add Transitions Between Animations
Now, we need to set up transitions between the animations.
- Create Transition from “Any State”: Right-click on Any State and choose Make Transition. Move your mouse over the Base state and click on it. This will create an arrow from Any State to Base animation.
- Transition from Base to RedAnim: Right-click on Base and choose Make Transition, then click on RedAnim.
- Transition from RedAnim to Base: Right-click on RedAnim and choose Make Transition, then click on Base.
This sets up the basic flow between the animations.
Step 12: Create a Trigger to Control Transitions
To make the transitions work through code, we need to create a trigger.
- In the Animator window, click the + icon at the top-left of the Animator window.
- Choose Trigger from the dropdown list.
- This creates a new trigger parameter that you can use in your code to control when to transition between the animations.
Step 13: Name the Trigger and Set Conditions
Now, we’ll name the trigger and set it to control the transitions.
- Name the trigger MakeRed. This will be the trigger we’ll call in C# to activate the transition.
- Left-click on the arrow going from Base to RedAnim. In the Inspector on the right, look for a section called Conditions.
- Click the + icon in the Conditions section and add the MakeRed trigger to the list of conditions. This will ensure that the transition from Base to RedAnim only occurs when MakeRed is activated.
- Do the same for the transition from RedAnim to Base. Left-click on the arrow, add the MakeRed trigger, and now Unity will know to only make these transitions happen if the MakeRed trigger is activated.
Step 14: Add the Animator Controller to the Cube
Now, let’s assign the Animator Controller to our cube.
- In the Inspector, drag and drop the CubeAnimator (or the name of your Animator Controller) into the Animator field of the cube’s Animator component.
Step 15: Turn Off Looping for Animations
Before we move on to the script, we need to disable the loop for the animations.
- In the Project window, left-click on each animation clip (e.g., RedAnim, Base, RedColorAnim).
- In the Inspector, uncheck the Loop Time box for each animation clip. This ensures that the animations will only play once and will not loop.
Step 16: Create a C# Script to Trigger Animations
Now, let’s create a C# script that listens for the spacebar press to trigger the color change.
- In the Project window, right-click and select Create > C# Script. Name the script RedScript.
- Open the script, and replace its contents with the following code:
using UnityEngine;
public class RedScript : MonoBehaviour
{
private Animator animator;
void Start()
{
// Get the Animator component attached to the Cube
animator = GetComponent<Animator>();
}
void Update()
{
// Listen for Spacebar press
if (Input.GetKeyDown(KeyCode.Space))
{
// Set the MakeRed trigger to transition between the animations
animator.SetTrigger("MakeRed");
}
}
}
This script grabs the Animator component from the cube and listens for the Spacebar key. When the Spacebar is pressed, it triggers the MakeRed parameter to fire the transitions.
Step 17: Attach the Script to the Cube
- Save the script and go back to Unity.
- Drag and drop the RedScript onto the Cube object in the Hierarchy window. This will attach the script to the cube.
Step 18: Test the Animation in Play Mode
- Press Play in Unity to start the game.
- Press the Spacebar while the game is running. The cube should change to red when the spacebar is pressed.
- Press the Spacebar again, and the cube should return to white.
Final Thoughts
By following this tutorial, you’ve learned the basics of using the Animator in Unity to create and control animations for GameObjects. You also learned how to trigger animations using C# and how to use triggers to control the transitions between animations.
Now you’re ready to experiment with more complex animation systems, blending animations, and integrating them with other game mechanics!