If you look back up to the right hand, you’ll see that we added it to the scene.
We’ll change that line.
varright_hand =newTHREE.Mesh(hand, cover);
right_hand.position.set(-150, 0, 0);
❶ scene.add(right_hand);
❶Change this line.
Instead of adding the hand to the scene, we add it to the avatar:
varright_hand =newTHREE.Mesh(hand, cover);
right_hand.position.set(-150, 0, 0);
❶ avatar.add(right_hand);
❶This line now adds the right hand to the avatar instead of the scene.
After doing the same for the left_hand, the right_foot, and the left_foot, your avatar
should be doing cartwheels—without losing any parts!
Sometimes we might not want our avatar to do cartwheels. Let’s add a line
to control that.
❶ varis_cartwheeling = false;
functionanimate() {
requestAnimationFrame(animate);
❷ if(is_cartwheeling) {
avatar.rotation.z = avatar.rotation.z + 0.05;
}
renderer.render(scene, camera);
}
animate();
❶This is where we say if our avatar is doing cartwheels or not. If we set this
to true, then our avatar is doing cartwheels. If we set it to false (like we’ve
done here), then our avatar won’t do cartwheels.
❷Wrap the avatar.rotation in an if, as shown. Don’t forget the curly braces on
this line and after the avatar.rotation line.
Change the value of is_cartwheeling from false to true. Does the avatar start
cartwheeling again?
report erratum • discuss
Doing Cartwheels • 33
Prepared exclusively for Michael Powell