private void Update() {
if (!_isControllable) {
// kill all inputs if not controllable.
Input.ResetInputAxes();
}
if (Input.GetButtonDown("Jump")) {
_lastJumpButtonTime = Time.time;
}
UpdateSmoothedMovementDirection();
// Apply gravity
// - extra power jump modifies gravity
// - controlledDescent mode modifies gravity
ApplyGravity();
// Apply jumping logic
ApplyJumping();
// Calculate actual motion
Vector3 movement = _moveDirection * _moveSpeed + new Vector3(0, VerticalSpeed, 0) + _inAirVelocity;
movement *= Time.deltaTime;
// Move the controller
CharacterController controller = GetComponent<CharacterController>();
_collisionFlags = controller.Move(movement);
// ANIMATION sector
if (_animation) {
if (_characterState == CharacterState.Jumping) {
if (!_jumpingReachedApex) {
_animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
_animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
_animation.CrossFade(jumpPoseAnimation.name);
} else {
_animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
_animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
_animation.CrossFade(jumpPoseAnimation.name);
}
} else {
if (controller.velocity.sqrMagnitude < 0.1f) {
_animation.CrossFade(idleAnimation.name);
} else {
if (_characterState == CharacterState.Running) {
_animation[runAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0f,
runMaxAnimationSpeed);
_animation.CrossFade(runAnimation.name);
} else if (_characterState == CharacterState.Trotting) {
_animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0f,
trotMaxAnimationSpeed);
_animation.CrossFade(walkAnimation.name);
} else if (_characterState == CharacterState.Walking) {
_animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0f,
walkMaxAnimationSpeed);
_animation.CrossFade(walkAnimation.name);
}
}
}
}
// ANIMATION sector
// Set rotation to the move direction
if (IsGrounded()) {
transform.rotation = Quaternion.LookRotation(_moveDirection);
} else {
Vector3 xzMove = movement;
xzMove.y = 0;
if (xzMove.sqrMagnitude > 0.001f) {
transform.rotation = Quaternion.LookRotation(xzMove);
}
}
// We are in jump mode but just became grounded
if (IsGrounded()) {
_lastGroundedTime = Time.time;
_inAirVelocity = Vector3.zero;
if (_jumping) {
_jumping = false;
SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
}
}
}