Loading lessons...
Root Motion and Scripting
Root Motion and Scripting
Root Motion allows an animation clip to drive the actual position and rotation of a GameObject's Transform, aligning visual movement with physical movement.
How Root Motion Works
Instead of moving a character using script inputs (like transform.Translate), the Animator reads the movement of the "root node" in the animation data and applies it to the character's Transform.
Controlling Root Motion in Script
To enable Root Motion:
- Select the GameObject with the Animator component.
- Check the Apply Root Motion option.
If you need to intercept and modify the motion data (for custom physics or collision resolution), implement the OnAnimatorMove() callback:
using UnityEngine;
public class CustomMovement : MonoBehaviour
{
private Animator _animator;
private Rigidbody _rb;
void Start()
{
_animator = GetComponent<Animator>();
_rb = GetComponent<Rigidbody>();
}
void OnAnimatorMove()
{
// Intercept root motion
Vector3 deltaPosition = _animator.deltaPosition;
Quaternion deltaRotation = _animator.deltaRotation;
// Apply to Rigidbody with custom velocity alterations
_rb.MovePosition(_rb.position + deltaPosition);
_rb.MoveRotation(_rb.rotation * deltaRotation);
}
}
Key Properties
Animator.deltaPosition: The change in position calculated from root motion this frame.Animator.deltaRotation: The change in rotation calculated from root motion this frame.
TL;DR
- Root Motion drives GameObject movement from animation files.
- Enable Apply Root Motion on the Animator component.
- Implement OnAnimatorMove to intercept and modify motion in scripts.