0 votes

I have some important elements in my game which could be used to gain massive velocity and essentialy break the game. So I want to set the max number for velocity.y. Also the game is in 3D so can't use clamp.

Godot version v3.3.2
in Engine by (46 points)
edited by

I don't understand why 3d stops you from using clamp

velocity.y=clamp(velocity.y, 0, max_speed)

Or better

velocity.y=min(max_speed, velocity.y)

Clamp can only be used in 2D.

clamp is a core script function, it takes a float value and return the value if inside the range, or min/max if outside the range.
3D, 2D, control nodes, resources, makes no difference

https://docs.godotengine.org/en/stable/classes/[email protected]#class-gdscript-method-clamp

it's like saying that sin(x) only works in 2D


you are probably confusing with the method Vector2(x,y).clamped(10), which returns a vector2 whose length is clamped.
in that case, you can easily workaround with the below formula (which is true both in 2D and 3D)

vec.clamped(value)=vec.normalized*min(vec.length(), value)

1 Answer

+2 votes
Best answer

I've had success clamping values without using clamp by setting it so that if it exceeds the max value, it automatically updates to that max value. Like this:

if velocity.y > 500: 
    velocity.y = 500

This wouldn't work in every case, but maybe it would work in yours?

by (166 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.