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

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;
            }
	}
}

(back to Mecanim introduction)

Page last updated: 2012-11-07