Game Development Reference
In-Depth Information
7. Next, we will create the
LateUpdate()
function to update the camera position
and rotation after all the objects have their
Update()
functions called. So, let's
add the following code:
Note
The
LateUpdate()
function will be called after the
Update()
function has
been called. This function will make sure that all the calculation in the
Up-
date()
function is finished before we start the
LateUpdate()
function. We
use the
LateUpdate()
function for the camera calculation because we don't
want the target position of the camera to lerp (linear interpolation) or get affected
by any concurrent physics or location calculations. It should be calculated after
the character's orientation and position has been determined in
Update()
.
For more details on this function, refer to
http://docs.unity3d.com/Documenta-
// Unity JavaScript user:
function LateUpdate () {
var targetCenter : Vector3 =
_targetTransform.position + _centerOffset;
var originalTargetAngle : float =
_targetTransform.eulerAngles.y;
var currentAngle : float =
_cameraTransform.eulerAngles.y;
var targetAngle : float = originalTargetAngle;
if (AngleDistance (currentAngle, targetAngle) > 160
&& _characterControl.IsMoveBackward) {
targetAngle += 180;
}
currentAngle = Mathf.SmoothDampAngle(currentAngle,
targetAngle, _angleVelocity, smoothTime, maxSpeed);
_targetHeight = targetCenter.y + height;
var currentHeight : float =
_cameraTransform.position.y;
currentHeight = Mathf.SmoothDamp (currentHeight,
_targetHeight, _heightVelocity, heightSmoothTime);
var currentRotation : Quaternion = Quaternion.Euler