How to set the max number for rigidbody.linear_velocity.length()?

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

My project uses a RigidBody with a sphere and I want to set the max speed, the RigidBody can go.

rigidbody.linear_velocity.length()
:bust_in_silhouette: Reply From: klaas

Hi.
For that you have to integrate a _integrate_forces ( PhysicsDirectBodyState state ) function to your RigidBody. Then you have to modify the PhysicsDirectBodyState.linear_velocity to limit the speed. Like so … (did not test)

var len = min(max_speed, state.linear_velocity.length())
state.linear_velocity = state.linear_velocity.normalize() * len

https://docs.godotengine.org/de/stable/classes/class_rigidbody.html#class-rigidbody-method-integrate-forces