Game Development Reference
In-Depth Information
After running the game for a while, you are going to see that the scene you have construc-
ted is no more visible to the camera as it moves higher and higher. Let's try to fix this little
problem by forcing the camera to always look at the center of the scene, regardless of its
current position. This is achieved by altering the rotation of the camera downwards as it
goes higher. Fortunately, we don't need to bother ourselves with the dirty details of this ro-
tation, since Unity provides this functionality directly through transform.LookAt() function.
Simply add the following line after line 15 in Listing 2.
What we expect to see now is vertical movement of the camera and rotation towards the
origin of the scene, which keeps the scene visible. If you start the game now, you are going
to get a change in the z position of the camera with the time. As the camera gets higher on
the y axis, it gets also closer to the center of the xz plane. This unexpected movement along
z axis can be justified by understanding the difference between translation in the local space
and translation in the world space .
By default, Translate() function is applied in the local space of the object, which is affected by its
position and rotation. World space, on the other hand, has fixed x, y, and z axes that are constant
among all game objects all the time. To understand the concept of local space, consider an
airplane object like the one in Illustration 14. This plane has a local space that is differ-
ent from the global world space. The right wing of the plane, which points to the positive
direction of the x axis of the local space. The plane front points towards the positive direction
of the z axis of the local space, and the positive y axis of the local space is perpendicular to
the upper surface of the plane and points upwards. (As a sort of motivation, we are going to
build this plane model and fly with it in the next chapter).
Back to our camera, when it rotates to look at the center of the scene, its local y axis
becomes no more parallel to the y axis of the world space as it originally was. Therefore,
when you move the camera along its local y axis, it going actually to move along the z axis
of the world space as well. Illustration 15 shows the local space axes of the camera after the
rotation.
Search WWH ::




Custom Search