+2 votes

I want to set the maximum velocity to a RigidBody2D, so it can't exceed the speed limit.

in Engine by (594 points)

1 Answer

+4 votes
Best answer
var max_speed = 100

...

if abs(get_linear_velocity().x) > max_speed or abs(get_linear_velocity().y) > max_speed:
    var new_speed = get_linear_velocity().normalized()
    new_speed *= max_speed
    set_linear_velocity(new_speed)
by (1,300 points)
selected by

Thanks for the answers.

I assume this would be used in integrate_forces().

If that's the case, I would also assume regardless of what you set linear velocity to, external forces such as gravity or other physics bodies will affect it after integrate forces. If so, the clamp only affects a body's speed before external forces are added to it. The actual total in-game velocity would look like this:

    totalLinearVelocity = clampedLinearVelocity + VelocityFromExternalForces

This presents a problem if you want to clamp the total velocity.

Is there a way to tell the rigidbody to not exceed a certain velocity when affected by external forces?

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.