Game Development Reference
In-Depth Information
currTargetPosition = currCamPos;
lastTargetPosition = currCamPos;
// Also set the lerp progress distance to 1.0f, which will tell the
lerping that it is finished.
// Since we set the target positionins to the camera's current
position, the camera will just
// lerp to its current spot and stop there.
currLerpDistance = 1.0f;
}
}
In the
Camera Controller (Script)
checkbox region in the
Main Camera
component
within the
Inspector
panel, you'll find the
Player Object
field. If you haven't yet,
make sure to drag the
Player
object into the
Player Object
field. This is shown in
the following screenshot:
Now, save your scene by pressing
Ctrl
+
S
or going to
File
|
Save Scene
, and then
play your game. As you move the player left and right, the camera will now move
with the player, stalking the player and observing the player—creepy! As you may
have already noticed, there is quite a lot of power we can now implement to the
camera thanks to the use of both a state machine and events/delegates. With the
previous code, not only can we track the player, but we could also implement states
that allow the camera to track other objects as needed. The camera will always
smoothly transition between the two objects without jumping around.
Currently, when the player reaches the ledge, they just keep going. Like a Vegas
magician earning his bread, the player walks across that huge empty gap like it's
nothing. It's high time we brought that player tumbling to the inky darkness below.