In this lesson, you will learn how to leverage Root Motion to ensure your character’s physical movement in the game world perfectly matches the motion authored in your animation clips.
The Core Concept: Animation Drives Position
Usually, when you move a character, you use a script to change the Transform.position via a CharacterController or Rigidbody. However, when an animator crafts a walk or run cycle, they often include subtle weight shifts and stride lengths that are hard to replicate perfectly with raw code. Root Motion solves this by telling Unity to extract the movement data directly from the "Root" bone of your animation and apply it to the actual GameObject in the scene.
Configuring the Animator
To enable this, you must first ensure your animation clips are set up correctly. In the Import Settings of your animation file, navigate to the Animation tab and ensure Root Transform Rotation and Root Transform Position (Y/XZ) are set to "Bake Into Pose" or "Based Upon" depending on your needs.
Once your clips are configured, select your Animator component on your character and ensure the Apply Root Motion checkbox is ticked. This tells Unity to move the object’s Transform based on the animation data rather than ignoring it.
Handling Movement via Script
Even with Root Motion enabled, you might still want to influence the movement via code. You can intercept the motion before it is applied to the object by using the OnAnimatorMove() callback in your script. This allows you to combine your own logic with the animation's inherent movement:
void OnAnimatorMove()
{
// Access the root motion applied by the animation clip
Vector3 deltaPosition = animator.deltaPosition;
Quaternion deltaRotation = animator.deltaRotation;
// Apply it to the character's transform or Rigidbody
transform.position += deltaPosition;
transform.rotation *= deltaRotation;
}
By manually applying animator.deltaPosition within OnAnimatorMove(), you maintain full control over the movement while retaining the perfect synchronization of the animation's stride and pathing.
Key Takeaway: Root Motion allows the animation itself to dictate the displacement of your character, eliminating the "sliding feet" effect and creating a seamless connection between visual movement and spatial positioning.