Facing the Proper Direction

Getting the avatar to face the proper direction is fairly easy—especially with all that we already know. Just as we did when we added the walking motion of the hands and feet, we’ll write a new function to turn our avatar. We’ll call this function turn, so let’s add a call to this function in the animate function.

 
function​ animate() {
 
requestAnimationFrame(animate);
 
walk();
 
turn();
 
acrobatics();
 
renderer.render(scene, camera);
 
}
 
animate();

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:

 

Get 3D Game Programming for Kids now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.