Clamping a vector3

Hi,
I’m trying to clamp a vector3, meaning clamping each individual value, and I’m reading contracting things on the internet.
I’ve tried various things but nothing works.

And I know I could use some convoluted method by clamping the x,y and z values individually, but there must be better way, and some official doc are saying this is possible, but again nothing I have tried worked.

You use vec.clamp(Vector3(x_min, y_min, z_min), Vector3(x_max, y_max, z_max)), or if you want the same range, use vec.clampf(min, max), see Vector3.clamp and Vector3.clampf

1 Like

This :

var vec:Vector3 = transform.basis.z * 50
vec.clamp(Vector3(0, 0, 0), Vector3(20, 20, 20))
print(vec)

and this

var vec:Vector3 = transform.basis.z * 50
vec.clampf(0,20)
print(vec)

Give me this :
(49.99463, -0.733049, -0.015252)
(49.99317, -0.826267, -0.015503)
(49.99158, -0.917714, -0.01577)

So clearly not working.

vec.clamp(0,20) (clamp and not clampf) straight up give me an error :
Cannot pass a value of type “int” as “Vector3”.

That’s exactly how it works. Here’s an explanation.

Very simple all-round camera controller. Weird jamming

Ir doesn’t modify the value, use vec = vec.clamp(...)

1 Like

This worked, thanks !

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