Game Development Reference
In-Depth Information
After moving the camera vertically, we check in lines 65 through 70 whether the new ver-
tical position of the camera is between the maximum and the minimum allowed values.
These values are defined by
minCameraHeight
and
maxCameraHeight
.Noticethatwecheck
thepositionusing
transform.localPosition
instead of
transform.position
. We do this in order to
make the position test relative to the vertical position of the player character rather than the
ground. This will make our computations applicable in all cases, including the cases where
the vertical position of the character changes, such as jumping case. We store the position
of the camera in
newCameraPosition
, then we check whether the member
y
of the new
position is within the allowed limits. If a modification is necessary we perform it, and we
finally store
newCameraPos
back in
transform.localPosition
in line 73.
Once we are done moving the camera, we need to make sure that the camera looks always
at the character. So, in line 76, we call
transform.LookAt()
and pass to it
playerBody
, which
refers to the character. In lines 81 through 93, we read the mouse wheel and interpret scroll
up as zoom in and scroll down as zoom out. Camera movement along its local z axis is
controlled by
zoomSpeed
and the position of the camera. This position after zooming must
be between
maxZoom
and
minZoom
. Once again we use
transform. localPosition
, since
camera zooming is performed against the character, rather than the world center. You can
construct a simple scene to test this input system, and you can also see the final result in
scene6
in the accompanying project.