Math question: get local velocity.z

I need to figure out something, but unfortunately my maths skills are deserting me.
What I need is the y velocity of a rigidbody, but not for worldspace, but for localspace, i.e. how fast is the rigidbody traveling forward?

I thought about rotating the rigidbody.velocity vector over the rotation of the transform and then read it’s y, but I couldn’t find a method to rotate a vector3. Where could I find a function like this? And is this the way to do it?

Thanks in advance!

I can’t believe I can’t find it anywhere in the docs:
How to rotate a vector3 ??

You can get the directional velocity with the dot product of rigidbody.velocity and the direction:-

var forwardSpeed: float = Vector3.Dot(rigidbody.velocity, transform.forward);

This works with any direction vector as long as it is normalised.

Thank you! Works perfectly.

Just for my general knowledge, if I have a Vector3 and a rotation (eg quaternion), how can I apply the rotation on the vector?

Of course it does :wink:

You can multiply a quaternion by a vector to get that effect - see here for details.

Or use Quaternion.LookRotation(Vector3)!