How to set max velocity.y of the player?

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

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.

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)

Andrea | 2021-08-25 21:26

Clamp can only be used in 2D.

99week | 2021-08-30 16:56

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

@GDScript — Godot Engine (stable) documentation in English

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)

Andrea | 2021-08-30 17:27

:bust_in_silhouette: Reply From: samjmiller

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?