Get "real" Velocity

So I would like to know the “real” velocity of my rigidbody, since I’m updating it every frame to move the character.

Unfortunately, if I push up against a wall, it still says I have the same velocity since I keep adding it.

Also, if I walk using both the “A” and “W” key, the velocity.magnitude says my velocity is larger than walking with just the “A” key. [Which is self explanatory, but I can’t seem to find a way around this]

Does anybody have a function to find the velocity of an object?

To prevent diagonal input from being greater than lateral input, you should normalize your input magnitude.

float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 frameInput = new Vector3( x, 0, z );
frameInput.Normalize();

Because you are writing to the velocity, naturally if you check it after writing to it, it’ll be the value you assigned.

You might be able to check the body’s true velocity before assigning to it, though unless your physics logic is in FixedUpdate() where it belongs, that might not work 100% of the time either.

Note that generally unless you have a very deliberate reason, you shouldn’t assign to a body’s velocity. Use the AddForce methods instead. Assigning to velocity essentially negates the purity of the physics simulation and can cause unexpected behavior when interacting with other bodies.

Velocity can be calculated by checking against the object’s position last frame;

// anywhere in Update()
Vector3 velocity = lastPosition - transform.position;
// at the end of Update()
lastPosition = transform.position;

For completeness sake it’s worth mentioning that if you want actual velocity you can simply store the transform position after each fixed update, then do distance over time. For reasons pointed out in other comments it’s not the best way to do the job.

I have solved it, thanks to AlwaysSunny.

I attempted TransformDirection, but it wasn’t working for some reason, but it was because I was doing the addition/subtraction in the wrong spot. Here’s the code:

move = new Vector3((input.x * speed), 0f, (input.z * speed));

rigidbody.AddForce((transform.TransformDirection(move) - rigidbody.velocity) * accel, ForceMode.VelocityChange);

I decided to use ForceMode.VelocityChange so I could calculate the acceleration myself.

Also, for the “real” velocity part, I used:

(int)Mathf.Clamp(rigidbody.velocity.magnitude, -speed, speed)

It’s an integer because it was for the crosshair script.

Since I am using VelocityChange, I might as well modify the velocity directly.