How would I go about converting a direction from Global space to Local space?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PrecisionRender


So I am trying to get a RigidBody3D’s linear velocity, more specifically, get the Z component of that so I can get my forward/backward speed. This works great… unless my RigidBody3D is rotated. Then the farther I rotate towards east/west, the less of a factor my Z component plays in telling me my forward speed.

How would I go about converting linear_velocity from a global direction to local one so I can always rely on the Z component of it being my forward velocity?


:bust_in_silhouette: Reply From: dethland

I think your question is a way to access the length of the velocity.
Simply, Vector3().length().
The Vector3 is your velocity vector.
This can give you the magnitude of your velocity which I assume is the “local z” you needed.
Wish it can help you.

Thank you for your help! I seem to have found something that also works:

linear_velocity.rotated(Vector3.UP, -rotation.y).z

If this causes any bugs I’ll be sure to try Vector3.length(). Again, thanks!

PrecisionRender | 2021-09-06 16:40