Graphics Reference
In-Depth Information
targetDirection+= vert * Vector3.forward;
// We store speed and direction seperately,
// so that when the character stands still we still have a
// valid forward direction
// moveDirection is always normalized, and we only update it
// if there is user input.
if (targetDirection != Vector3.zero)
{
moveDirection = Vector3.RotateTowards(moveDirection,
targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.
deltaTime, 1000);
moveDirection = moveDirection.normalized;
}
// Smooth the speed based on the current target direction
curSmooth= speedSmoothing * Time.deltaTime;
// Choose target speed
//* We want to support analog input but make sure you can't
// walk faster diagonally than just forward or sideways
targetSpeed= Mathf.Min(targetDirection.magnitude, 1.0f);
_characterState = CharacterState.Idle;
// decide on animation state and adjust move speed
if (Time.time - runAfterSeconds > walkTimeStart)
{
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;
// Calculate actual motion
Vector3 movement= moveDirection * moveSpeed;
movement *= Time.deltaTime;
// Move the controller
collisionFlags = controller.Move(movement);
// Set rotation to the move direction
myTransform.rotation = Quaternion.LookRotation(moveDirection);
}
void UpdateRotationMovement ()
{
// this character movement is based on the code in the Unity
// help file for CharacterController.SimpleMove
// http://docs.unity3d.com/Documentation/ScriptReference/
// CharacterController.SimpleMove.html
Search WWH ::




Custom Search