Is there a way to limit a variable?

Godot Version

4.2.1

Question

I want to make a value (speed) to stop increasing or decreasing when it gets to a specific value (max_speed). I’m currently using this:

`speed += acceleration * direction * delta

if speed > max_speed:
	speed = max_speed
if speed < -max_speed:
	speed = -max_speed`

But I’m not sure if it’s ok or if there’s a way to simplify it. Thanks!

Use clampf(speed, -max_speed, max_speed)

I tried and it didn’t work

Did you do:

speed = clamp(...)
1 Like

Ah! Now it works. Thank you!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.