Game Development Reference
In-Depth Information
As you know from reviewing the Scripting Reference (*cough cough* You DID review it, right?)
the
OnAnimatorMove()
function is called every frame after the state machines. This occurs before
OnAnimatorIK()
, where IK stands for “Inverse Kinematics,” another animation method available in
Unity Pro.
(3) var animator : Animator = GetComponent(Animator);
if (animator)
The
animator
variable is null if an Animator Component is not attached to the zombie game object,
so in the following line it is used in the conditional
if
statement. If the game object has an animator,
the following block of code will be executed, but if there is no animator, it is skipped.
(4) var newPosition:Vector3 = transform.position;
Within the conditional, the
newPosition
Vector3 type variable is declared, then assigned the current
transform position of the zombie game object.
(5) newPosition.x += WalkX * Time.deltaTime;
newPosition.z += WalkZ * Time.deltaTime;
The transform's x and z coordinates are updated each frame by the
WalkX
and
WalkZ
factors and
smoothed with the now familiar
Time.deltaTime
.
(6) transform.position = newPosition;
Finally, the zombie game object's transform position is updated with the new x and z property
values.
Save the script. In the Unity editor, drag the script from the Property panel to the zombie_lowres
game object in the Hierarchy view to attach it. Two things happen in the Inspector when you
do this. The ZombieMotionScript appears as a component, as you expect. Additionally, the
Animator component's Apply Root Motion changed to Handled by Script when it detected the
OnAnimationMove()
function within the script (Figure
5-34
). Save the scene and the project.
Figure 5-34.
Newly attached ZombieMotionScript component, and Handled by Script update in the Animator component's Apply
Root Motion property