Game Development Reference
In-Depth Information
11. As a final step, we point the
LookAt
GameObject reference of the camera to
the center of
trackObject
so that it is always precisely in the middle of the
field of view. It is most important to never lose the object you are tracking in
a 3D game. This is critical!
transform.LookAt
(trackObj.transform.position);
Congratulations! We have now written our first
camera
class that can smoothly track
a rotating and translating object. To test this class, let's set the following default val-
ues in the
Inspector
pane as seen in the previous screenshot:
•
TrackObj
: Set this to the
Player1
object by dragging-and-dropping the object
reference from the
Hierarchy
tab to the
trackObj
reference in the object
inspector.
•
Height
: Set this to
0.25
. In general, the lower the camera, the more dramatic
the effect but the less playable the game will be (because the user can see
less of the world on screen).
•
Desired Distance
: Set this to
4
. At this setting, we can see the character
framed nicely on screen when it is both moving and standing still.