x


Problems with Character Controller velocity

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?

more ▼

asked Aug 04 '10 at 08:28 AM

cyangamer 1 gravatar image

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

Sep 26 '11 at 01:13 PM markhula
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1047
x676
x318

asked: Aug 04 '10 at 08:28 AM

Seen: 1872 times

Last Updated: Sep 26 '11 at 01:13 PM