Having problem with getting RigidBody.velocity.x or velocity.z...

Hello, I’m using Unity3d’s third person character controller. I’m having a problem in where the velocity.x and the velocity.z come out differently than the velocity…

Here’s a video of what’s happening:

The velocity.x and .z go up, then when the character is stopped they fall down then the float goes into some other number and I can’t figure out why it’s happening. I’ve tried so far turning the camera off and on, gravity, is kinematic, the same thing is happening. I’ve also tried FixedUpdate, LateUpdate and Update. .magnitude is doing the same thing…

using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.ThirdPerson;

public class Object_Graphics : MonoBehaviour {
    private ThirdPersonCharacter tPC;

    Start()  = {
      tPC = GameObject.Find ("ThirdPersonController").GetComponent<ThirdPersonCharacter> ();
   }

   LateUpdate() {
    
    //this is reading out differently...
    Debug.Log ("Rigidbody velocity = " + tPC.m_Rigidbody.velocity);
    ///than this...
    Debug.Log ("Rigidbody velocity.x = " + tPC.m_Rigidbody.velocity.x);

   }

}

I’m considering just use a StartingValue and EndingValue and using tPC.transform.position.x and tPC.transform.position.z and a timer to get velocity at this point by comparing the two values.

The Debug.Log() of a Vector3 (the .ToString()) is rounded differently than a float is, that’s why you’re seeing slightly different values. They’re not actually different.

So would I have to round the float? I guess I’m not understanding why the float comes out the value that it does, because I can’t use it for movementVelocity…

I tried taking the values and doing something like this trying to create characterMovementVelocity out of the values…

            if (characterMovementVelocity <= 1f) {
                animationState = AniState.Idle;
            }
            if (characterMovementVelocity > 1f && characterMovementVelocity <= 4.5f) {
                animationState = AniState.Walking;
            }
            if (characterMovementVelocity > 4.5f) {
                animationState = AniState.Running;
           }

But it messes in accordance with what the float was in Debug.Log…So I’m having a hard time just getting a single number for movement velocity.

There’s no reason something like that shouldn’t work, but comparing a vector to a float doesn’t really make any sense. You probably want to test against its magnitude.

if(characterMovementVelocity.magnitude <= 1f)
// etc etc
1 Like

@GroZZleR

Oh, right you actually gave me the solution in your first post but I didn’t read between the lines so to speak.

I need to round the floats:

newMagnitude = (magnitude * 100f) / 100f;

Why? The second post from @GroZZleR is correct - if you want to compare the velocity (which for the purpose of how your animation controller seems to look is just speed you’re interested in), compare the magnitude (“length” of the vector) of the rigidbody.velocity directly. No need to calculate anything on your own, especially since you’re comparing only against ranges for idle/walking/running.

Btw - for code clarity your 3 if’s should be an if/elseif/elseif - only one of them should run at any point in time, no need to check conditions for others. This has an added benefit of not running potentially costly calculations for the checks that will never be true anyway.

@Kalladystine

Well, I don’t know why but for some reason the code I had posted in my second post didn’t work until I rounded the float. I don’t know why that is, but that’s just how it is. It works fine, now, though.