Game Development Reference
In-Depth Information
Step 3. When there are a series of animation files accompanying a
model, as is the case here, Unity finds them all and adds them to the
Animation component of the Inspector. As can be seen in Figure 3.8 ,
this model has over 20 different animations. The animation set at the
very top is the one that plays by default. It is the current animation .
Change the current animation to WalkForward.
Step 4. Play. The character will take one step.
Step 5. To make the animation loop, create a new JavaScript file called
animationController and add the code shown in Listing 3.8 .
Listing 3.8 Script to Make the Current Animation Loop When
the Game Object Is Created
function Awake()
this.animation.wrapMode = WrapMode.Loop;
Step 6. Save and play. The model will walk on the spot. The
WalkForward animation has a start and an end frame pose that matches.
This allows the animation to play over and over again seamlessly. To see
what an unmatched animation looks like, set the current animation to
WalkIdle and run it again. You will notice a frame jump between the
end of one loop and the start of the next.
Step 7. Add a rigidbody and a capsule collider to the model.
As shown in Figure 3.9 , position the capsule collider so that the
model's feet appear to rest on the ground. For the rigidbody,
freeze the x , y , and z axis rotation. This will stop the model from
toppling over.
Step 8. Open the animationController.js script and modify as shown in
Listing 3.9 .
Listing 3.9 Adding Forward, Backward and Idle Animations
private var speed = 0.01;
function Awake()
this.animation.wrapMode = WrapMode.Loop;
function Update ()
this.transform.position = this.transform.forward * speed;
else if(Input.GetKey("down"))
Search Nedrilad ::

Custom Search