This recipe will cover some principles on how to make the camera follow something in the game world. While it might seem like an easy task at first, there are some tricky bits too.
The recipe will build upon the Creating an RTS camera AppState object recipe of this chapter. All of the steps described in this recipe will be applied to AppState
.
To make the camera follow units, perform the following steps:
We start by adding two new variables, which we'll use for the new functionality. A Vector3f variable, called
targetLocation
, will be used to track the target, and a Boolean variable calledfollow
, will be used to declare whether the camera should track the target or not. These are set from external classes.Out of convenience, we also define a final Vector3f variable, called
UNIT_XZ
, which we set to(1f, 0, 1f)
. We'll use this to convert 3D positions to 2D.Then, we need to add some functionality in the
update
method just beforecam.setLocation...