Notes: Animation

So let’s talk about the structure of the animation system in Unity. There’s a few major thing to make clear of: Animator, Avatar, Animation Controller and Animation Clip.

Animator is the component we add to the game object we want to animate, it’s the interface to the Animator Controller and the Avatar we want to use.

The Avatar is basically the description of the model, specifically the bones in the model and how they’re can move.

The Animation Controller, as it name suggest, controls the animation. It decides which animation clip should be play, how they should be play, and which part of the model the animation should be play on (we can have multiple animation playing on different part of the model at the same time using avatar mask).

The Animation Clip is a series of keyed positions of each part(bone) of the avatar/model, playing them in a sequence makes animation. In my understanding, the animation clip can be categorized into two type, repeatable animation and one-time animation. The repeatable animation includes idle, walking, running where the movement loops, and is supposed to be played in a loop as well. The one-time animation are like impact, attack, die, jump where the movement doesn’t need to loop (but they can, and better if they do) and supposed to be played once and followed up by another animation.

Now we got those out of the way, let’s dive into how to actually set them up. In the following notes, I’m going to focus on animating a Humanoid model. First of all we need to make an avatar of the model we want to animate. If the model is bought or downloaded, it’s likely that the avatar is included and saves us time from doing so. In case that it’s not, simply open the model in the inspector, go to Rig tab, select Humanoid as the Animation Type, select Create From This Model in the Avatar Definition, and Configure. I won’t go into further detail of rigging the avatar, but put it in simple word, we just need to match the bones with the mesh.

Next, we’ll need to get some animation clips, a good place to get them for free is mixamo.com. After downloading the file(should be in .fbx format), import it and open it in the inspector. Go to the Rig tab, select Humanoid and copy from the Avatar we had, then apply it. We only need the animation clip, so expand the file in the projects directory, duplicate the animation clip named “mixamo.com” (not the Take 001). That’ll be the animation clip we want, rename it so we know which animation it is.

After getting all the animation we want, we need to create an Animation Controller to manage all these animation. Assuming we have the Idle, walking and running as our repeating animation. We need to create a blend tree, which is responsible to transition between those animation seamlessly when the character accelerate from standing still to walking to running. Right click in the background and choose Create State > From New Blend Tree. Double click on it, and open the blend tree in the inspector. On the left side, add a “Speed” float parameter (or rename the “Blend” parameter if it exist). Make sure the blend type is 1D (or 2D if we have animations for left and right movement). Add the three animation clips (Motion field) to the Motion list. If the animations are out of order, simply rearrange them in the list.

Back to base layer, we want to rename the blend tree to Locomotion, and make sure the Entry point is transitioning into it directly(should be done by default). Now we can take a look of how these locomotion animations looks. In the character game object, add a “Animator” component, and drag in the Animation Controller and the Avatar. The character should be playing Idle animation if the game is playing. Increase the “Speed” parameter to see it transition from idle to walking and running. We can adjust this programmatically using

animator = GetComponent<Animator>(); // cache this in Start()
agent = GetComponent<NavMeshAgent>(); // assuming using NavMeshAgent


animator.SetFloat("Speed", agent.velocity); // in Update() or FixedUpdate()

Now it’s time for one-time animations. For these, drag them to the base layer, they’ll form a state each. However, there’s no transition to them from the Locomotion State. To do this, we need to have triggers, in the parameter, add a “Attack” trigger parameter. Next, right click on the Locomotion State, select Make Transition and link it to the state of the Attack animation clip. Select the transition, and deselect the “Has Exit Time” in the inspector, and in the conditions, add the “Attack” trigger. We can start the attack animation in code:

animator.SetTrigger("Attack");

End of note. 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *