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).

Finally, to control the motion, we will need to create a script (RootMotionScript.cs), that implements the OnAnimatorMove callback:-

using UnityEngine;
using System.Collections;


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;

You should attach RootMotionScript.cs to the "Dude" object. When you do this, the Animator component will detect that the script has an OnAnimatorMove function and show the Apply Root Motion property as Handled by Script

(back to Mecanim introduction)

Page last updated: 2013-10-10