How far has the player moved in one direction?

This seems simple enough, but I’m a bit stuck…

I want to calculate the distance the player has moved in a particular direction per frame to update the score (the player gains points for the farther they move in the correct direction).

The direction is a Vector3 that changes every frame (the course is curvy). The player is a Rigidbody, so I can access velocity as well.

I’ve been trying to calculate the percent that the player velocity matches the direction and use that like this:

float degreesDiff = Vector3.Angle(nDirection, nVelocity);
float percentDiff = 1.0f - (degreesDiff / 360.0f);
float movementForward = rigidbody.velocity.magnitude * percentDiff;

No surprise, this doesn’t work (straffing pumps up the velocity magnitude and then the percent doesn’t mater).

What am I missing guys? Can I use a dot product or something for this?

Cheers

You can probably multiply the rigidbody.velocity.magnitude with Vector3.Dot(nDirection, rigidbody.velocity.normalized) and get a reasonable approximation. It’ll give you a negative if you go in the wrong direction, of course.

That’s what I was looking for.

Thanks!