3D Game Programming

(C. Jardin) #1
Next write the function turn(). JavaScript doesn’t care where you put this
function, but since we call it after walk() in the animate() function, we might as
well put it after the walk() function. Type the following after the closing curly
brace of the walk() function:

functionturn() {
vardirection = 0;
if(is_moving_forward) direction = Math.PI;
if(is_moving_back) direction = 0;
if(is_moving_right) direction = Math.PI/2;
if(is_moving_left) direction = -Math.PI/2;

avatar.rotation.y = direction;
}

With that, when we walk left or right, the avatar now faces the direction in
which it’s moving:

That is pretty amazing. You have now made a complicated game avatar. Think
about what you’ve accomplished:


  • Given the avatar a body, hands, and feet

  • Made the avatar move so that all the pieces move with it

  • Made the avatar do cartwheels and flips

  • Stuck the camera to the avatar

  • Stuck the camera to the avatar’s position so that flips and cartwheels
    don’t make us dizzy

  • Made the hands and feet swing back and forth when the avatar walks

  • Made the hands stop moving when the avatar is not moving

  • Made the avatar face the direction that it’s walking


Chapter 8. Project: Turning Our Avatar • 80


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf