clamp() in godot 4.0 not working

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

I tried to clamp players max velocity using clamp() but it keeps returning MAX_SPD value. MAX_SPD is set to 5

velocity.x = clamp(velocity.x , MAX_SPD, -MAX_SPD)
velocity.z = clamp(velocity.z , MAX_SPD, -MAX_SPD)

print(velocity.x , " " ,velocity.z)

What is printed if you do not clamp the velocities?

magicalogic | 2022-12-23 05:07

everything works fine except player speed exceeds max_spd, if i dont use clamp just the velocity gets printed. When i use clamp both velocities are stuck at 5.

Tiez | 2022-12-23 06:02

:bust_in_silhouette: Reply From: Ninfur

The second argument should be the min value, and the third argument should be the max value.

Try:

velocity.x = clamp(velocity.x , -MAX_SPD, MAX_SPD)
velocity.z = clamp(velocity.z , -MAX_SPD, MAX_SPD)