How to set a maximum speed for a KinematicBody2D that is falling due to gravity?

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

In the KinematicBody2D script I have these lines of code:
enter image description here

I am completely new to GDScript, and what I understood from the code above is that it increases the velocity.y by 30 thirty times or so every second. So I would like to have it stop increasing the velocity.y at some point.

If it is easier to just make the fall speed constant, instead of using the code I already have, that should work too.


:bust_in_silhouette: Reply From: exuin

First off, you should multiply gravity by delta, which is the time elapsed from the last frame, so your physics aren’t frame-rate dependent. Second, just do an if statement.

if velocity.y < some_number:
    velocity.y += GRAVITY * delta

[+= is the same as adding the value directly to the variable]

:bust_in_silhouette: Reply From: ponponyaya

This is another way to solve this problem.
You can use original codes, and add the following code line after it.

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

Then the max value of velocity.y is 1000, and min is 0.

Note that 0 and 1000 is just for example, you can choose a number you like.