In this first recipe, we will get familiar with Blend Trees (a new type of states in the
Animator Controller). Blend Trees allow to smoothly blend multiple animations together. A common example for using them is blending walk and run cycles.
Getting ready
Before we start, you should have a character with at least three looped animations: idle, walk (in place), and run (also in place). You can download the provided example; open the project in Unity and go to the Chapter 04 Character movement\Recipe 01 Using blend trees to blend walk and run animations folder. You will find a scene called Example.unity there, with an animated humanoid character. If you play the game, you can click on the ground to move the character, and if you press Shift while moving, the character will blend to run animation smoothly. In the Rigs directory, you can find
the Humanoid.fbx asset with the required animations.
How to do it…
To create a smooth blend between walk and run animations, follow these steps:
Import your animated character with Idle, Walk, and Run animations. Walk 1. and Run animations should be done “in place“.
Set the animations to Loop Time in the character asset's Inspector in
2. the Animation tab. If your character's rig is set to Humanoid, you may also need to set all the Bake Into Pose options to true for each animation and all the Based Upon options to Original, as shown in the following screenshot:
Settings of in place Idle, Walk and Run animations
Create an Animator Controller for your character.
3.
Drag and drop the Idle animation into the Animator Controller to make it the 4. default state.
Add a float Speed parameter in the Animator Controller.
5.
Right-click on the empty space in the Animator Controller and choose Create 6. State | New From Blend Tree. This will create an empty Blend Tree.
Click on the Blend Tree and change its name in the Inspector to something 7. meaningful; we use WalkAndRunBlend in this example.
Double-click on the Blend Tree. It will open the Blend Tree settings.
8.
Click on the plus button (marked with number 1 in the following screenshot) and 9. choose the Add Motion Field option twice. Two Motion fields will be added.
Drag and drop your WalkInPlace animation in the first (upper) field and 10. your RunInPlace animation in the second (lower) field. Fields are marked with
number 2, as shown in the following screenshot:
Blend Tree properties
Unselect the Automate Thresholds option (3 in the preceding screenshot). This 11. will make the Threshold field available for each Motion field. These are the
thresholds of the Parameter used by the Blend Tree (marked with 4 in the preceding screenshot). If the Parameter is set to the exact value of a given threshold, the animation corresponding to that threshold is played. If the Parameter is set to a value between two thresholds, both animations are played with appropriate weights (and are blended together).
Set the Thresholds to 1 for the WalkInPlace motion and 4 for 12. the RunInPlaceMotion (you may need to adjust these values later).
Click on the Play button in the Preview window, below the Blend Tree settings.
13.
Find the Speed parameter slider on the Blend Tree node, and move it to see the 14. animations blending smoothly, depending on the Speed parameter value.
Double-click on the empty space in the Animator Controller to get out of 15. the Blend Tree settings.
Create two transitions:
16.
Idle | WalkAndRunBlend with the condition set to Speed parameter greater than 0.5, Has Exit Time set to false, and Transition Duration set to 0.2 seconds.
WalkAndRunBlend | Idle with the condition set to Speed parameter less than 0.5, HasExitTime set to false, and TransitionDuration set to 0.2 seconds.
Write a script to move your character and set the Speed parameter value
17. according to the movement speed of your character. We are using point and click movement in this example. You can find two scripts in the provided Unity project, ClickToMove.cs in the Shared scripts directory
and SetSpeedFromAgent.cs in the Scripts directory of this recipe. The first one is used to set the destination for the Nav Mesh Agent component attached to the character.
In the Update() function of the SetSpeedFromAgent.cs script, we use
18. the desiredVelocity of the NavMeshAgent to set the Speed parameter in our Animator Controller:
anim.SetFloat("Speed", agent.desiredVelocity.magnitude, 0.2f, Time.deltaTime);
The anim variable stores the reference to the Animator component of the 19. character and the agent variable stores the reference to the Nav Mesh Agent
component.
We are also adjusting the agent.speed variable in the Update() function to set 20. the maximum speed of the NavMeshAgent. This allows us to make the character
run when player holds the Shift button and walk when player releases the Shift button:
if (Input.GetKey(KeyCode.LeftShift) ||
Input.GetKey(KeyCode.RightShift)) {
Blend Trees blend multiple animations based on the chosen Animator Controller parameter value and Motion fields' thresholds. In the standard Blend Tree, always only two
animations are blended at the same time: the animation with a lower Threshold and the animation with a higher Threshold. Each animation is blended with the weight
corresponding to the distance between the actual parameter value and the Threshold of the animation. For instance, if your Walk animation Threshold is set to 1, your Run
animation Threshold is set to 2, and your current Speed parameter value equals 1.5, both Walk and Run animations will be played with 50 percent weight, which will most likely result in your character jogging slowly.
When blending Walk and Run animations, make sure to have the contact poses in the same normalized time in both animations. For instance, if the Walk animation has contact poses in 0 percent, 50 percent, and 100 percent of the animation, the Run animation should also have contact poses in 0 percent, 50 percent, and 100 percent of the animation. That will assure proper and smooth blending between the animations.
There's more…
There are a few other interesting options in the Blend Tree settings:
You can set the Blend Type of a Blend Tree to the following:
1D: A simple Blend Tree using one parameter to define the weight of a currently played animation.
2D Simple Directional: This option uses two parameters for blending the animations, such as the X axis and Y axis. It is best used for motions representing different movement directions, for instance, walk forward, walk left, walk right, and walk back. It shouldn't be used with multiple animations representing movement in the same direction (such as walk forward and run forward).
2D Freeform Directional: This option is similar to the previous one, but you can use multiple animations representing movement in the same direction (such as walk and run). You need to add a single animation representing the motion in the 0, 0 position (such as idle).
2D Freeform Cartesian: This option is similar to the preceding one, but is used when your animations don't represent movement in different directions. It can be used to blend multiple versions of the idle animation, for instance.
Direct: You can control the weight of each of the nodes (Motion fields) directly. This type is often used while blending facial expressions (see the Animating facial expressions with Blend Shapes recipe in Chapter 5, Character Actions and Expressions).
You can set a few additional Motion field options:
Time Scale: You can alter the playback speed of each animation in the Blend Tree by changing the number in the Time Scale field:
the one with a clock icon. It is set to 1 (100 percent) by default.
Mirror: You can mirror any of the animations in the Blend Tree by checking the Mirror option, the one with a mirrored humanoid icon.
You can also check the Automate Thresholds option, which will distribute the Motion fields' Thresholds evenly throughout the whole parameter's range.
For instance, if your parameter's range is 0 to 9 and you have four animations, the first one will have a Threshold of 0, the second one 3, the third one 6, the fourth one 9. You can change the parameter range by clicking on the 0 and 1 numbers below the blending graph.
You can also use the Compute Thresholds option (Automate Thresholds have to be set to false). This option will compute the Thresholds based on the root motion information from your animations (speed magnitude, velocity in the X axis, velocity in the Y axis, velocity in the Z axis, angular speed in radians, and angular speed in degrees). We will cover root motion in the next recipe.