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

: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

1 Like