6.3 Swinging Hands and Feet Together
How did it work? Were you able to get all of the hands and feet swinging back
and forth? Did you run into any problems?
If you tried moving the hands and feet in the same way, you might have
noticed that our avatar is moving awfully strangely. Both feet and both hands
move forward at the same time. And then both feet and both hands swing
back at the same time. No one walks like that in real life.
When you walk, one foot is in front and the other is behind. In avatar terms,
one foot is in the positive Z direction while the other is in the negative Z
direction:
varposition = Math.sin(clock.getElapsedTime()*5) * 50;
right_foot.position.z = -position;
left_foot.position.z = position;
People also usually move their right hand forward when their left foot is for-
ward. And if the right hand is forward, then the left hand should be back. We
can make our avatar do this with the following.
functionwalk() {
varposition = Math.sin(clock.getElapsedTime()*5) * 50;
right_hand.position.z = position;
left_hand.position.z = -position;
right_foot.position.z = -position;
left_foot.position.z = position;
}
With that, our hands and feet should be swinging back and forth in a nice
walking motion.
6.4 Walking When Moving
Right now, our avatar is constantly walking—even when we’re not controlling
it with our controls from Chapter 4, Project: Moving Avatars, on page 35. Let’s
fix this problem.
First we add one line to our walk() function.
report erratum • discuss
Swinging Hands and Feet Together • 63
Prepared exclusively for Michael Powell