[SOLVED]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?

Make three variables :
previousPosition, type Vector3
moveLength, type float
moveSpeed, type float

Before the move, set previousPosition = transform.position.

After the move, set moveLength = Vector3.Distance (previousPosition - transform.position)

Then, moveSpeed = moveLength / Time.deltaTime

moveSpeed (if required) should represent the true speed per second, and the true velocity (if you are walking against a wall, moveSpeed should be very low, or null).

1 Like

Well, that didn’t directly fix the problem (spat out the same value), but I did figure out the issue:

the speed that the function was getting was, for one reason or another, dependent on the direction of the game object. Looking closer, it looks like the CharacterController capsule collider that’s attached to the GO is screwing up the velocity because of the sliding (this is just a guess). I made the capsule bigger and decreased its Y position, and that fixed the problem a little bit (I don’t know why), but now the GO’s levitating in the air! Now I need to figure out how to make a CharactherController collide correctly on a disk-shaped object.

I do find it odd that no matter where the GO runs into my character, it always slides to the same point in the 3D space.

Well, that’s for your reply, AkilaeTribe. We at the forums appreciate your help. :slight_smile:

I will post about my new problem soon.