You've probably already noticed that sometimes when our character stops walking, it blends to the Idle animation in a strange way (with sliding feet). That is mostly visible when legs in the Walk animation are in a pose that looks like a mirrored Idle pose. We are going to fix this problem in this recipe.
Getting ready
You should use the same character as in the previous recipe and prepare one additional animation that will be a transition from a mirrored Idle pose to a normal Idle pose. You can also go to the Chapter 04 Character movement\Recipe 04 Using animations for better looking transitions directory and open the Example.unity scene. You will find our Humanoid character working the same way as in the previous recipe, but with a ToIdle animation added. You can also find the rig with all required animations in the Rigs directory.
How to do it…
To use additional animations for a better-looking transition, follow these steps:
First, follow all the points from the Using root motion to steer a character recipe to 1. have a working character driven by root motion animations.
In the Animation Import Settings of the character, find your additional ToIdle 2. animation. You may need to add it as a new clip by clicking on the plus button in
the Clips section (Animations tab) and choosing the ToIdle animation as the source.
Check the Bake Into Pose option for Root Transform Rotation and Root 3. Transform Position (Y) in the ToIdle animation.
Open the Animator Controller (this should be the same controller as in the 4. previous recipe), add another float parameter, and call it ToIdle.
Go back to the Animation Import Settings and find the WalkForward animation.
5.
In its import settings, go down to the Curves section, unfold it, and click on the 6. plus icon to add a new Animation Curve.
Name it ToIdle (its name should match exactly the parameter we created in the 7. Animator Controller in step 4). Click on the curve (it's a flat green line at the
moment) to open the Curve Editor. Curves are described by float values on the vertical axis and normalized animation time on the horizontal axis.
Create a square-shaped curve with its maximum value equal to 1 and its 8. minimum value equal to 0 (point 1 in the following screenshot). You can add
curve control points by clicking on the curve. You can select points by clicking on them. You can remove points with the Delete key. To adjust the handles and interpolation types of a point, right-click on it. To set a square curve, choose Both Tangents | Constant. You can save a Curve Preset for later use by clicking on the small gear icon in the lower left corner of the Curve Editor (see point 2 in the following screenshot). When you scrub through the timeline (point 3 in the following screenshot), you can see where your curve starts in the normalized animation time (point 4 in the following screenshot):
Animation Curve and the Curve Editor
In our example, the Idle animation has its left foot in front and our ToIdle 9. animation starts with its right foot in front. During the animation, the character
makes a small step to go back to the proper Idle pose with its left foot in front. In the WalkForward animation settings, try to set the curve value to 1, on the passing pose, when the right foot passes the left foot (and will be in front of the character few frames later).
Save the curve as a Curve Preset and add it to the WalkLeft and WalkRight 10. animations. Set their curves names to ToIdle as well.
Go to the Animator Controller.
11.
Add the ToIdle animation and create new state transitions:
12. Steering | ToIdle with two conditions, the Speed parameter less than 0.5 and the ToIdle parameter set to greater than 0.5. Has Exit Time should be set to false and Transition Duration should be set to 0.2 seconds.
ToIdle | Idle with no conditions, Has Exit Time set to true, and Transition Duration set to 0.2 seconds.
Alter the Steering | Idle transition by adding a second condition to it: the ToIdle 13. parameter set to less than 0.5. See the following screenshot:
Animator Controller using an additional ToIdle transition animation
Start the game and try walking and stopping the character in different time of the 14. walk animation. You should see that the character plays the ToIdle animation to
prevent foot sliding in the most visible case of the walk pose being a mirrored idle pose.
How it works…
There are a few key elements of this recipe:
Animation Curves: You can add curves to your animations (multiple curves per animation clip) in the Import Settings, Animation tab. Curves assign arbitrary float values to the animation frames. If the name of the curve is the same as the name of a parameter in the Animator Controller, the value of the parameter will be set to the value of the curve in any given frame of the animation.
Using curve values: We are using the ToIdle value set by the Animation Curves in the WalkForward, WalkLeft, and WalkRight animations to trigger the
transition from Steering to Idle (when the ToIdle value is less than 0.5) or from Steering to ToIdle animation (when the ToIdle parameter is greater than 0.5). This allows us to control the state transitions in the Animator Controller depending on the poses in the animations.
There's more…
We've covered the most simple (yet most visible) case in this recipe. You can add more stop animations (or any transition animations) from different walk and run poses. You may need to add a stop animation for walk and run passing poses and for all the extremes, depending on your Idle pose. To do so, add more curves to your animations and react to them in the Animator Controller. For instance, you may have curves for
LeftFootExtreme, RightFootExtreme, LeftFootPassing, and RightFootPassing;
use them in your Walk and Run animations to trigger appropriate stop animations.
You can also check the value of an Animation Curve in runtime by checking the corresponding parameter value of the Animator Controller in scripts. To do so, use the animator.GetFloat() method, where animator is the reference to the Animator component.