Introduction
Root motion helps your character move naturally by using the animation's own movement instead of coding it manually.
Jump into concepts and practice - no test required
Root motion helps your character move naturally by using the animation's own movement instead of coding it manually.
animator.applyRootMotion = true;
applyRootMotion to true on your Animator component to enable root motion.Animator animator = GetComponent<Animator>(); animator.applyRootMotion = true;
OnAnimatorMove to customize how root motion affects the GameObject.void OnAnimatorMove() {
// Custom root motion handling
transform.position += animator.deltaPosition;
transform.rotation *= animator.deltaRotation;
}This script enables root motion on the Animator. The character moves exactly as the animation dictates without extra code.
using UnityEngine;
public class RootMotionExample : MonoBehaviour {
Animator animator;
void Start() {
animator = GetComponent<Animator>();
animator.applyRootMotion = true;
}
void Update() {
// No manual movement needed, animation moves the character
}
}Root motion only works if the animation clip has movement in its root bone.
Disabling root motion means you must move the character manually in code.
Use OnAnimatorMove to fine-tune or combine root motion with code movement.
Root motion lets animations control character movement for natural results.
Enable it by setting applyRootMotion to true on the Animator.
You can customize root motion by overriding OnAnimatorMove.
applyRootMotion on an Animator component do in Unity?applyRootMotion to true allows the animation's movement data to move the character automatically.applyRootMotion to enable root motion.animator.applyRootMotion = true; is valid C# syntax and correct property usage.void OnAnimatorMove() {
Vector3 rootPos = animator.rootPosition;
rootPos.y = transform.position.y;
transform.position = rootPos;
}
What is the effect of this code on the character's movement?void OnAnimatorMove() {
transform.position = animator.rootPosition;
transform.rotation = animator.rootRotation;
}
But the character does not move as expected. What is the likely problem?applyRootMotion is true on the Animator component.OnAnimatorMove to override root motion is valid, but it requires applyRootMotion enabled.OnAnimatorMove to achieve this?OnAnimatorMove to set horizontal position from root motion and add vertical velocity manually to Y position.