Graphics Reference
In-Depth Information
{
targetSpeed *= runSpeed;
_characterState = CharacterState.Running;
}
else
{
targetSpeed *= walkSpeed;
_characterState = CharacterState.Walking;
}
moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
// Reset walk time start when we slow down
if (moveSpeed < walkSpeed * 0.3f)
walkTimeStart = Time.time;
}
Update() begins with a call to stop input when canControl is set to false. More than
anything, this is designed to bring the player to a stop when canControl is set at the end of
the game. When the game ends, rather than inputs continuing and the player being left to
run around behind the game-over message, inputs are set to zero:
void Update ()
{
if (!canControl)
{
// kill all inputs if not controllable.
Input.ResetInputAxes();
}
UpdateSmoothedMovementDirection(), as described earlier in this chapter, calls one
of two movement update scripts, based on the value of the Boolean variable moveDirec-
tionally. After this is called, the final part of the Update() function takes care of coor-
dinating the animation of the player (provided that there is a reference to an animation
component in the variable _animation):
UpdateSmoothedMovementDirection();
// ANIMATION sector
if(_animation) {
if(controller.velocity.sqrMagnitude < 0.1f) {
The controller's velocity is used to track when it is time to play the idle animation. The
_characterState will not come into play if velocity is less than 0.1f, since we can guarantee
at this velocity that the idle animation should be playing above anything else. Animation.
CrossFade() transitions from the current animation to the new one smoothly or continues
to play the current animation if it is the same as the one being passed in.
The actual animation data are stored in their own variables of type AnimationClip:
runAnimation, walkAnimation, and idleAnimation. Note that Animation.CrossFade
uses the name of the animation rather than the object:
_animation.CrossFade(idleAnimation.name);
}
else
{
Search WWH ::




Custom Search