Managing Animations with the Animator Component

It's time to start implementing animated 3D characters into our Unity projects!

To begin with, we would need a 3D modelled character and at least two animations to transition between. It would be sensible to download a character with idle and walk animations.

We can download appropriate 3D models and animations from www.mixamo.com.

Mixamo is an online platform that provides a vast library of free 3D character models and animations. It allows developers to download ready-made characters and animations such as walking, running, jumping, and many more. Users can easily apply these animations to their characters, thus saving time and effort in the animation process. Mixamo is especially useful for Unity projects as it offers seamless integration and compatibility with the Unity engine.

Tutorial to Import 3D Models from Mixamo

Configuring our Animation Files

Once we have downloaded two files, one with an animation, another animation without skin, we can drag the two files into Unity's Asset section:

Note that one file has a 3D model preview, and the other does not. We can click on our file with the 3D model to extract the textures from the file and attach them to the model:

Click 'Extract Textures' here.

You can now grab the file with the 3D model and drag it into the scene:

Our animations, by default, don't loop. The two animations we are using, idle and walk, would both be better if set to loop.

For each of the two files, select them and choose the 'Animations' tab in the insector:

Then tick 'Loop Time' and then 'Apply':

You can also rename your animation clips too if necessary using the text field just above the 'Loop Time' boolean.

Setting up the Animator Component

Next, we need to attach the animations to the 3D model. We can do this using a component called 'Animator'. Attach it by selecting Add Component on the object inspector as you would any other component:

Next, we need to create an object in the Assets section called an 'Animation Controller'. This is an object that manages what animations are played at any time. Right click in the Assets section, and choose Create > Animator Controller:

As you can see, it looks like two cubes connected with cables.

Drag the Animator Controller into the 'Controller' field on the Animator component:

Next, we need to now open the Animator window in Unity to start editing the Animator. Click Window > Animation > Animator:

Then, if you select an object in the Hierarchy that has the Animator component, it will show that Animator in that new window:

The first thing we are going to do in the Animator is to right-click anywhere and create a Blend Tree. Choose Create State > From New Blend Tree:

Blend trees allow us to transition smoothly between two or more animations with variables. Once you've created a blend tree, it should appear in your Animator:

Note that we have this transition now from the 'Entry' node to the 'Blend Tree' node. This simply means that this node will be the first animation to be loaded when the game starts. Think of this is as the 'Start()' message, but for animations.

Double-click on the blend tree to edit it:

When you click on the Blend Tree node in your Blend Tree, you will see that on the right of the screen, in the Inspector, there is a section called 'Motion' that says the 'List is Empty'. Click the plus icon here and add two 'Motion Fields':

When you add two motion fields to a blend tree, a parameter is used to transition and blend between these animations smoothly. This parameter acts as a slider, allowing the blending of animations based on its value.

Attach your two animations in this project into the motion fields. You can click the little circle at the side of the field to go through the animation clips in the project. The idle animation should be first (at threshold 0) and the walk animation should be second (at threshold 1).

This allows you to drag the 'Blend' variable left and right to transition smoothly between those two animation states!

Controlling Animations in C#

Finally, we need a way to control the animation using some logic. We can do this easily with a Blend Tree by changing the 'Blend' parameter. In this specific case, we are going to control that parameter with keyboard input. When we push the 'W' key, we will transition to walking. When we release the W key, we will return gradually back to idle.

Make a script and attach it to the same object that has the Animator component attached:

In this script, I want to be able to change some parameters on the Animator component. This is similar to how we called functions (e.g. AddForce()) in the Rigidbody component. We therefore need to use the 'GetComponent()' functions to gain access to the Animator:

    //Make a variable to hold the component.
    Animator anim;
    
    void Start()
    {
        //put this specific object's component into the variable.
        anim = GetComponent<Animator>();
    }

This will allow us to control the Animator component when typing our anim variable. Specifically, we want to control the Blend parameter using player input.

In our Update() section then, we can use the following script:

anim.SetFloat("Blend", Input.GetAxis("Vertical"));

The Animator has a function called 'SetFloat'. Set float requires two arguments. It first needs you define which float you want to set/change. You have to specify that float using a string (quotation marks). The second argument is what you want to set that float to. Specifically, I want to change this to the return value of a function in the Input class called GetAxis().

Unity Axis Manager

Unity is constantly checking the player is pressing any keys, or moving their mouse. That information is saved inside of 'Input Axes'.

The default input axes can be found by clicking Edit > Project Settings, Input Manager:

Let's expand the 'Vertical' axis:

As you can see, we control this axis using either the up and down arrows, or the s and w keys. Specifically, we will return a +1 value if either w or up arrow are pressed, a -1 value if down or s keys are pressed, and a 0 if none of those keys are pressed. More importantly though, the values gradually move from 0 to 1/-1. This is controlled by the Gravity/Sensitivity options. These are both set to 3, which means it takes a third of a second to go from 0 to 1/-1.

Can you incorporate multiple animated characters? Can you control multiple character's animations with different input axes?

Reference Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimationManagement : MonoBehaviour
{
    //Container to put the Animator component into.
    Animator anim;
    void Start()
    {
        //Grab the Animator component, put it inside our container.
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        //Change the 'Blend' parameter in the Blend tree to whatever is in the Axis called 'Vertical'.
        anim.SetFloat("Blend", Input.GetAxis("Vertical"));
    }
}

Last updated