Graphics Reference
In-Depth Information
currentRotationAngle is a float representing the angle, in degrees, that we need to
turn the camera at to try to reach the target rotation in wantedRotationAngle. It's an
interpolation carried out by Mathf.SmoothDampAngle.
Mathf.SmoothDampAngle is an interpolation function designed specifically for eas-
ing between two angles. It will automatically cap the return value between 0 and 360 as
well as choose the correct way around (if your value is at 181°, it would interpolate up
rather than down so that the rotation has a natural flow and doesn't go back on itself).
The Unity documentation tells us that SmoothDampAngle takes the following
parameters (in order of how they should be passed in):
current
The current position.
target
The position we are trying to reach.
currentVelocity
The current velocity; this value is modified by the function
every time you call it.
smoothTime
Approximately the time it will take to reach the target. A
smaller value will reach the target faster.
maxSpeed
Optionally allows you to clamp the maximum speed.
deltaTime
The time since the last call to this function. By default Time.
deltaTime.
In this part of the code, the yVelocity variable holds the current velocity of the inter-
polated move, but it is not actually used anywhere else in the script. It's a required field for
SmoothDampAngle, so we just provide it for the function to work right and forget about
it. Note that one possible use for getting the velocity may be speed limiting the move,
but SmoothDampAngle can actually take care of this with no extra code by providing a
maxSpeed parameter. This is useful for limiting interpolation speeds without having to
do it yourself with velocity.
The currentRotationAngle is, in effect, the rotation amount required around the y -axis
(hence using the name yVelocity to describe its speed). The variable rotationSnapTime
decides how much time it should take for the rotation to happen, which is public and
designed to be set in the Unity editor Inspector window on whichever gameObject the
script is attached to:
currentRotationAngle = Mathf.SmoothDampAngle(currentRotation
Angle, wantedRotationAngle, ref yVelocity, rotationSnapTime);
The height movement is interpolated using Mathf.Lerp, which takes three param-
eters: the start amount, the target amount, and a time value clamped between 0 and 1.
We use the float heightDamping, multiplied by Time.deltaTime, to make the transi-
tion time based. Are you wondering why do we need to make the time parameter time
based? Good question! How the time parameter of Mathf.Lerp works has been a very
popular question on various forums and help pages. The time value represents how much
of a portion, of the difference between two values, that should be applied each time this
Search WWH ::




Custom Search