Unity Manual>User Guide>Creating Gameplay>Mecanim Animation System>Bringing Characters to Life>Mecanim Advanced topics>Root Motion - how it works>Tutorial: Scripting Root Motion for "in-place" humanoid animations
Sometimes your animation comes as "in-place", which means if you put it in a scene, it will not move the character that it's on. In other words, the animation does not contain "root motion". For this, we can modify root motion from script. To put everything together follow the steps below (note there are many variations of achieving the same result, this is just one recipe).



OnAnimatorMove callback.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animator))]
public class RootMotionScript : MonoBehaviour {
void OnAnimatorMove()
{
Animator animator = GetComponent<Animator>();
if (animator)
{
Vector3 newPosition = transform.position;
newPosition.z += animator.GetFloat("Runspeed") * Time.deltaTime;
transform.position = newPosition;
}
}
}
OnAnimatorMove and Apply Root Motion property shows up as Handled by Script

(back to Mecanim introduction)
Page last updated: 2012-11-07