Clamp a Vector

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

I have a velocity vector and I need to clamp it to the top speed. I assume I can use velocity.length() to get the current speed of the character relative to its direction in the velocity vector, but how do I clamp it? You cannot set the length of a vector to a number so I cannot use the clamp function. I’m sorry if this was confusing, any help would be beneficial. Thanks

1 Like
:bust_in_silhouette: Reply From: jgodfrey

I assume you’re looking for Vector2.clamped()

See Vector2 — Godot Engine (stable) documentation in English

Yup, that works, I’m not sure how I missed that. Thanks!

djmick | 2022-06-03 19:31

2 Likes

Hey to anyone in 2026 looking for a way to clamp your vector’s total length or magnitude, it looks like they changed the function to vector.limit_length()

5 Likes