0 votes

Hello,

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?

Thanks.

Godot version v4.0.dev.calinou [036b7a098]
in Engine by (113 points)

1 Answer

+1 vote
Best answer

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.

by (315 points)
selected by

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.