Graphics Reference
In-Depth Information
For horizontal input, the variable horz is multiplied by the variable rotateSpeed and
multiplied by Time.deltaTime to be used as the rotation for the player's (myTransform) y
rotation:
void UpdateRotationMovement ()
{
myTransform.Rotate(0, horz * rotateSpeed * Time.deltaTime, 0);
Vertical input (stored in vert) is dealt with next, as vert multiplied by moveSpeed
makes up the speed at which to traverse the player's z -axis.
Note that moveSpeed is calculated further down in the function, so don't worry about
that right away—just keep in mind that moveSpeed is the desired speed of movement:
curSpeed = moveSpeed * vert;
CharacterController.SimpleMove moves a character controller, taking into account
the velocity set by the vector passed into it. In this line, the vector provided to SimpleMove
is the player's forward vector multiplied by curSpeed:
controller.SimpleMove( myTransform.forward * curSpeed );
targetDirection is a Vector3-type variable that determines how much, based on the
vert input variable, we would like to move, even though its vector is only used in calculat-
ing for its magnitude later on in the function:
// Target direction (the max we want to move, used for calculating
// target speed)
targetDirection= vert * myTransform.forward;
When the speed changes, rather than changing the animation instantly from
slow to fast, the variable curSmooth is used to transition speed smoothly. The variable
speedSmoothing is intended to be set in the Unity Inspector window, and it is then multi-
plied by Time.deltaTime to make the speed transition time based:
// Smooth the speed based on the current target direction
float curSmooth= speedSmoothing * Time.deltaTime;
The next line of the script caps the targetSpeed at 1. Taking the targetDirection vector
from earlier, targetSpeed takes a maximum value of 1 or, if it is less than 1, the value held
by targetDirection.magnitude:
// Choose target speed
//* We want to support analog input but make sure you can't walk
// faster diagonally than just forwards or sideways
targetSpeed= Mathf.Min(targetDirection.magnitude, 1.0f);
The rest of the function transitions between walking and running in the same way
that the UpdateDirectionalMovement()function did earlier in this section:
_characterState = CharacterState.Idle;
// decide on animation state and adjust move speed
if (Time.time - runAfterSeconds > walkTimeStart)
Search WWH ::




Custom Search