clamped() doesn't work (godot 4.0)

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

Hello, I’m trying to learn godot4.0 using HeartBeast action RPG tutorial (which is on the older version of Godot) and everything was fine until I tried to use clamped()

am I missing something?

:bust_in_silhouette: Reply From: zhyrin

The godot editor has a built-in documentation.
You can Ctrl+Click on a type (Vector2 in this case) and see it’s entire description.
It says it doesn’t have a clamped() function, you can confirm it for yourself.
Instead it has a function called clamp(), but it doesn’t take the same kind of parameter.

Checking out the online documentation (specifically the 3.x) version, you can see in the description of Vector2.clamped() that it is depricated, and limit_length() should be used instead. This function is present in the 4.0 version as well.

As a side note, CharacterBody2D has a built-in velocity member, you don’t need to define it yourself.

zhyrin | 2023-03-24 09:36

thanks a lot ! yes I had to use limit_length() instead of clamped(), it works perfectly fine now.

terebi | 2023-03-24 09:49

1 Like

Hello, thanks a lot this helped me as well. I am following the tutorial

Also I have one more question.

I used limit_legnth() but my character moves slow and the input is only 1.6. -

You should remove the delta from line 21. On line 16 and 18 you limit your max velocity and multiplying by delta which is good; but you multiply by delta again on line 21, which is unneeded.

delta, in movement controllers, is used to make velocity consistent regardless of the frame rate. Because delta is usually a small fraction, it has the side-effect of decreasing velocity when multiplying.

1 Like