Animate your Player character
Tutorial
·
Beginner
·
+10XP
·
30 mins
·
(43)
Unity Technologies

In this tutorial, you’ll add animations for walking and idle states to bring your player character to life.
1. Overview
In this tutorial, you'll create an Animator Controller to define how your player's animations are structured, when they transition, and how they behave. You'll assign this controller to the Player GameObject and link it to your PlayerMovement script. This will allow you to control animations through code, so that the idle animation plays when the character is still, and a walking animation plays when the player presses a movement key.
2. Create the Animator Controller
In Unity, the Animator component allows GameObject to use animations. Animator components work together with Animator Controllers, which define how the animations transition from one to another and under what conditions. In this step, you'll create a basic Animator Controller from scratch and assign it to the Player GameObject.
1. Create an Animator Controller:
- In the Project window, navigate to _3DStealthGame > Animations.
- Right-click the Animations folder, select Create > Folder, and rename it “Animators”.
- Right-click the Animators folder and select Create > Animation > Animator Controller.
- Name the new Animator Controller “Player_Controller”.
2. Make sure you're in prefab editing mode:
If you're not in prefab editing mode, either double-click the Player prefab in the Project window or click the arrow next to the Player GameObject in the Hierarchy window.
3. Add the Animator Component to the Player prefab:
- In the Inspector window, select the Add Component button.
- Search for and add the Animator component.
- Click and drag the Player_Controller Animator Controller you created into the Controller property box in the Animator component.
4. Configure the Animator Settings:
- Avatar: Ensure this property is set to [Character]_ModelAvatar.
- The Avatar defines the skeleton and bones used by the model. Without it, animations won’t apply correctly.
- Animate Physics: Ensure this property is enabled.This keeps animation updates in sync with Unity's physics engine, making movement smoother and more predictable.
- Update Mode: Ensure this property is set to Fixed.
- This ensures the animation updates happen in the same rhythm as the physics system — ideal when using Rigidbody components.
- Culling Mode: Ensure this property is set to Cull Update Transforms.
- This saves performance by stopping visual updates when the character isn’t visible, but still updates transforms (e.g., for logic or physics).
5. Exit prefab editing mode
When finished, return to your normal scene view by exiting prefab editing mode. To do this, click the left-facing arrow located at the top of the Hierarchy window, to the left of Player.
In the next step, you'll create animation states inside the Animator Controller.
3. Set up the animation states
Animation states represent individual animations—like walking, jumping, or idling—that a character can switch between during gameplay. These states are defined and managed inside the Animator Controller. In this step, you'll define a simple state machine with just two states: Idle and Walking. This setup will allow your player character to switch between standing still and moving, depending on user input.
1. Open the Animator window:
- Double-click the Player_Controller Animator Controller to open it in the Animator window.
- The Animator window is divided into two key areas:
- On the left-most side, you’ll see the Animator Layers and Parameters tabs.
- On the right-most side, you'll find the Base Layer pane where you’ll create and connect animation states.
2. Add Animation States to the Animator Controller:
- In the Project window, navigate to _3DStealthGame > Animation > Animations.
We’ve provided two different idle animations and two walking animations for your character; feel free to choose the ones you like best.
- Expand the following model assets:
- Character@Idle1
- Character@Idle2
- Character@Walk1
- Character@Walk2
- Inside each model asset, locate the animation clips named Idle1, Idle2, Walk1, and Walk2.
- To preview the animations, select each one. In the Inspector window, scroll down to find the Preview section, then press the Play button to see how it looks.
Note: You’ll notice that each animation plays with a different character model. But don’t worry, both Player characters share the same skeleton (that does sound a little weird), which basically means they have the same body structure underneath. So the animations are fully compatible and will work perfectly on both models.
- Once you’ve chosen your favorite Idle and Walking animations. First, drag the Idle animation file into the Animator window. Then, repeat for the Walk animation.
- Each animation will be added as a new state in the Animator (displayed as a rectangle).
3. Understand the Animator Controller basics:
- The default state is shown in orange, and usually is the first state you added; in this case, Idle.
- If needed, you can right-click on any state and select Set as Layer Default State to change which one plays first.
4. Review the current setup:
- If you enter Play mode right now, the player character will stay in the Idle state permanently because the Animator Controller doesn’t yet know when to transition.
At this point, your Animator Controller has two animation states—Idle and Walking—but no logic to switch between them. In the next step, you'll create transitions and define the conditions that control when the player character switches from Idle to Walking and back again.
4. Create animation transitions
Animation parameters are variables used by the Animator Controller to decide when to transition between different animation states. These parameters are set and updated through code during gameplay. By creating logical connections between parameters and transitions, you can control when the player character switches animations; for example, from idle to walking.
In this case, you’ll create a boolean parameter named IsWalking. Boolean values can only be true or false, making them perfect for conditions like “is the character walking?” When the player presses a movement key, IsWalking will be set to true, triggering the walking animation. And when no keys are pressed, IsWalking becomes false, and the character returns to the idle animation.
1. Create a new animator parameter:
- In the Animator window, with your Player_Controller Animator Controller still open, locate the Parameters tab.
- Open the Add (+) dropdown and select Bool.
- Name the new parameter “IsWalking”.
Note: Be sure to use the exact spelling and capitalization for your new parameter.
2. Create transitions between states:
- To create the Idle to Walking transition:
- Right-click the Idle state and select Make Transition.
- Select the Walk state to define the transition end point.
- To create the Walking to Idle transition:
- Right-click the Walk state and select Make Transition.
- Select the Idle state to define the transition end point.
3. Configure transition settings (Idle to Walk):
- Select the arrow between Idle and Walk in the Animator window.
- In the Inspector window, disable Has Exit Time.
Note: This ensures the transition happens immediately based on input, instead of waiting for the animation to finish.
- Under Conditions, select the Add (+) button.
- Ensure the condition reads: IsWalking is true.
4. Configure transition settings (Walk to Idle):
- Select the arrow between Walk to Idle in the Animator window.
- In the Inspector window, disable Has Exit Time.
- Under Conditions, select the Add (+) button.
- Ensure the condition reads: IsWalking is false.
5. Test the parameter behavior:
- Make sure both the Scene view and Animator window are visible. For this:
- Click and drag the Animator window next to the Game view to dock it at the bottom.
- Enter Play mode.
- In the Animator window, locate the IsWalking parameter.
- Toggle the IsWalking checkbox on and off to see your character switch between Idle and Walk animations in real time.
- Exit Play mode when you're done testing.
- Click and drag the Animator window next to the Scene view to get it back to its original position at the top.
Now your animations are set up to respond to a parameter! In the next step, you’ll connect this IsWalking variable to your PlayerMovement script so that everything happens automatically when the player presses a movement key.
5. Add references to the animator parameter in your script
In the previous tutorial, you created the PlayerMovement script that allows the player to move the player character using input keys (arrow keys or WASD keys). In this step, you'll enhance the script by connecting it to the Animator component. Specifically, you’ll reference the IsWalking parameter so that it gets updated based on player input; switching to true when keys are pressed, and back to false when they’re released, and that each state triggers the corresponding animation.
1. Open the PlayerMovement script:
- In the Project window, navigate to _3DStealthGame > Scripts.
- Double-click the PlayerMovement script to open it in your IDE.
2. Declare the Animator variable:
At the start of your PlayerMovement class, declare a new variable to store the Animator component you added to the Player GameObject in the last steps:
public class PlayerMovement : MonoBehaviour
{
Animator m_Animator;
.
.
.3. Get the Animator component in Start():
Inside the Start() method, add the following line of code to retrieve the Animator component attached to the GameObject:
m_Animator = GetComponent<Animator> ();4. Check input and set the parameter:
Inside the FixedUpdate() method, add the following code after the m_Movement.Normalize(); line:
bool hasHorizontalInput = !Mathf.Approximately (horizontal, 0f);
bool hasVerticalInput = !Mathf.Approximately (vertical, 0f);
bool isWalking = hasHorizontalInput || hasVerticalInput;
m_Animator.SetBool ("IsWalking", isWalking);This code checks if the player is pressing any movement keys (horizontal or vertical). If there is input, it sets the IsWalking parameter in the Animator to true, which triggers the walking animation. If there’s no input, it sets the IsWalking parameter to false.
Note: This is a simplified explanation of the code's purpose.
5. Save your script:
- Make sure all changes in your PlayerMovement script are saved using the shortcut Ctrl+S (macOS: Cmd+S).
In the next step, you’ll test this setup in Play mode and fine-tune the speed and alignment between movement and animation.
6. Test and troubleshoot the animation
Now that your script is working, it’s time to test it in action. You’ll likely see that the basic functionality is correct: your player character moves and the animation responds to input. However, you might notice a slight mismatch between the walking animation and the actual movement speed.
To fix this, you’ll adjust the animation’s Speed or Transition Duration. This ensures that the player character’s visual motion aligns smoothly with its physical movement, making the experience feel more responsive and realistic.
1. Test and troubleshoot in Play mode:
Enter Play mode and observe the player character’s movement and animation closely.
If the walking animation feels out of sync (either too fast or too slow) compared to the movement:
- Go to the Animator window.
- Select the Walking animation state or the transition in question.
- In the Inspector window, adjust the Speed or Transition Duration property values until the animation visually matches the movement of the character.
Note: If you didn’t change any default values, we’ve found that setting the Transition Duration from Walk to Idle to 0.197 gives the best results.
2. Save your project:
- Once you’re happy with how it looks and feels, save your project to preserve the changes using the command Ctrl+S (macOS: Cmd+S).
With these simple adjustments, your player character now has properly aligned animations, making movement and gameplay feel more natural and polished.
If you’re curious to learn more, check out Unity’s Animation Documentation or the Creative Core Pathway: Animation course.
7. Next steps
With these steps completed, your player character now has a properly implemented animation system that syncs with movement.
In the next tutorial, you’ll set up a camera that follows the player character around as they move.