protected new void Update () {
//Get velocity in world-space
Vector3 velocity;
if (canMove) {
//Calculate desired velocity
Vector3 dir = CalculateVelocity (GetFeetPosition());
//Rotate towards targetDirection (filled in by CalculateVelocity)
RotateTowards (targetDirection);
dir.y = 0;
if (dir.sqrMagnitude > sleepVelocity*sleepVelocity) {
//If the velocity is large enough, move
} else {
//Otherwise, just stand still (this ensures gravity is applied)
dir = Vector3.zero;
}
if (navController != null) {
#if FALSE
navController.SimpleMove (GetFeetPosition(), dir);
#endif
} else if (controller != null)
controller.SimpleMove (dir);
else
Debug.LogWarning ("No NavmeshController or CharacterController attached to GameObject");
velocity = controller.velocity;
} else {
velocity = Vector3.zero;
}
//Animation
//Calculate the velocity relative to this transform's orientation
Vector3 relVelocity = tr.InverseTransformDirection (velocity);
relVelocity.y = 0;
if (velocity.sqrMagnitude <= sleepVelocity*sleepVelocity) {
//Fade out walking animation
anim.Blend ("forward",0,0.2F);
} else {
//Fade in walking animation
anim.Blend ("forward",1,0.2F);
//Modify animation speed to match velocity
AnimationState state = anim["forward"];
float speed = relVelocity.z;
state.speed = speed*animationSpeed;
}
}
}