Graphics Reference
In-Depth Information
moveSpeed is an interpolation (using Mathf.Lerp) between its current value and the
variable targetSpeed over an amount of time set by curSmooth.
When moveSpeed drops below the threshold set by walkSpeed (essentially, when the
player releases the move input enough to slow movement down), walkTimeStart gets reset
so that the walk will start again next time the player moves:
moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
// Reset walk time start when we slow down
if (moveSpeed < walkSpeed * 0.3f)
walkTimeStart = Time.time;
The final part of the UpdateDirectionalMovement() function tells the character con-
troller to move and sets the rotation of myTransform to match the movement direction.
In the original Unity third-person controller script this code was adapted from, this code
was in the Update() function. It was moved here for the purpose of keeping all directional
movement code within a single function.
The variable movement takes moveDirection and multiplies it by moveSpeed; then,
on the next line, the result is multiplied by Time.deltaTime to provide a time-based
movement vector with magnitude suitable for passing into the character controller.
When the controller is told to Move (with the movement vector passed in as a param-
eter), it will return a bitmask called CollisionFlags. When a collision occurs with the
character controller, the collisionFlags variable may be used to tell where the collision
happened; the collisionFlags mask represents None, Sides, Above, or Below. Although
the collision data are not used in this version of BaseTopDown.cs, it is implemented for
future functionality:
// Calculate actual motion
Vector3 movement= moveDirection * moveSpeed;
movement *= Time.deltaTime;
// Move the controller
collisionFlags = controller.Move(movement);
In the last line of the function, Quaternion.LookRotation creates a rotation toward
moveDirection and sets myTransform.rotation to it—making the player face in the correct
direction for the movement:
// Set rotation to the move direction
myTransform.rotation = Quaternion.LookRotation(moveDirection);
}
The second type of movement in BaseTopDown.cs is rotational movement.
UpdateRotationalMovement() uses a combination of transform.Rotate and the character
controller's SimpleMove function to provide a control system that rotates the player on its
y -axis and moves along its z -axis (in the case of the humanoid, this is forward and back-
ward walking or running).
Search WWH ::




Custom Search