Game Development Reference
In-Depth Information
In professional-grade games, the animated movement of game objects is typically a combination of
animation-based Root Motion, scripted motion, physics, and more. Precisely how these are applied
depends on the game, but always the goal is to provide the best game experience—the most
realistic or style-appropriate, consistent, responsive animation.
OnAnimatorMove
You can use scripts to provide motion for animation clips without Root Motion or to further
customize Root Motion within an animation clip with OnAnimatorMove. For the finest control you
would want to create and use animation curves that are available in Unity Pro, but the following
example will give you a taste of controlling character movement directly with scripting.
For this example, from the Project panel create a new Animator Controller and call it
ZombieMotionController. Drag the walking animation clip to the Animator window, where it
should appear orange as the new default state. Select the zombie_lowres game object in the
Hierarchy view, then change the Controller in its Animator component either by dragging the
ZombieMotionController to the Controller field or using the circle select button to the right of the
Controller field. Play to confirm the zombie is walking in place.
In the Project panel, create a new script with Create ➤ Javascript and name it ZombieMotionScript.
Double-click to open it in MonoDevelop. Delete the empty Start() and Update() functions, then add
the following code:
public var WalkX:float = 0;
public var WalkZ:float = 0;
function OnAnimatorMove()
{
var animator : Animator = GetComponent(Animator);
if (animator)
{
var newPosition:Vector3 = transform.position;
newPosition.x += WalkX * Time.deltaTime;
newPosition.z += WalkZ * Time.deltaTime;
transform.position = newPosition;
}
}
This breaks down as follows:
(1) public var WalkX:float = 0;
public var WalkZ:float = 0;
You are declaring public float variables for the x and z component of the zombie's transform position
so you can adjust them in real time in the Game view.
(2) function OnAnimatorMove()
 
Search WWH ::




Custom Search