Game Development Reference
In-Depth Information
change, we smooth out the current rotation and height more with a smaller
dampening value, and we tighten the interpolation with a larger value.
Also note that we multiply
heightDamp
by
Time.deltaTime
in order to
make the height interpolation frame rate independent, and instead depend-
ent on elapsed time, as follows:
RotAngle = Mathf.LerpAngle
(RotAngle, DesiredRotationAngle,
rotDamp);
Height = Mathf.Lerp
(Height, DesiredHeight, heightDamp *
Time.deltaTime);
10. The fourth and last step in our
GameCam
algorithm is to compute the position
of the camera.
Now that we have an interpolated rotation and height, we will place the cam-
era behind
trackObject
at the interpolated height and angle. To do this,
we will take the facing vector of
trackObject
and scale it by the negative
value of
desiredDistance
to find a vector pointing in the opposite direction
to
trackObject
; doing this requires us to convert
eulerAngles
to
Qua-
ternion
to simplify the math (we can do it with one API function!).
Adding this to the
trackObject
position and setting the height gives the
desired offset behind the object, as shown in the following code:
Quaternion CurrentRotation =
Quaternion.Euler
(0.0f, RotAngle, 0.0f);
Vector3 pos =
trackObj.transform.position;
pos -=
CurrentRotation * Vector3.forward *
desiredDistance;
pos.y = Height;
transform.position = pos;