I have an NPC with a character controller that roams around. It's important that I know its velocity so that the right animation clip will be playing. I'm getting an error in my program where my model is not moving, but the velocity is ~10, so the "walk" animation is playing. I should note that the NPC is currently being purposely blocked by my character, so the NPC's Move() function is being called, but it's not actually moving. As I said, I need this functionality in order to have the right animation clip play.
Here is the NPC's move function:
public void MoveTowards(Vector3 position)
{
Vector3 direction = position - transform.position;
direction.y = 0;
if (direction.magnitude < 0.2F)
{
if (num == (waypoints.Length - 1)) num = 0;
else num++;
return;
}
// Rotate towards the target
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
// Modify speed so we slow down when we are not facing the target
Vector3 forward = transform.TransformDirection(Vector3.forward);
float speedModifier = Vector3.Dot(forward, direction.normalized);
speedModifier = Mathf.Clamp01(speedModifier);
// Move the character
direction = forward * speed * speedModifier;
controller.SimpleMove(direction);
}
And here's is the block of code that handles animation, inside the Update() method:
if (gameObject.GetComponent<CharacterController>().velocity.magnitude > 1)
{
animation.CrossFade("walk");
}
else
{
animation.CrossFade("idle");
}
Can anyone help?
asked
Aug 04 '10 at 08:28 AM
cyangamer 1
40
●
6
●
6
●
13
Hi,
I have the same problem.
character.velocity.magnitude is > 0 though the character is colliding ; assuming for some reason the magnitude isn't being updated correctly on the collision; can anyone help?
thanks