Better Feeling Jumping

Godot Version

4.51

Question

I know its probably a dumb question but im trying to get better and not use ai at it so like, how can i make my jump so that the longer you hold it the higher you go and if you do a short tap its a short jump. (in a 3rd person platformer btw)

1 Like

This makes me very happy, so much so that I created an account to help you out :slight_smile:

So, assuming your Jump code is something like the following (this will be your “high jump”):

const JUMP_VELOCITY = -300

if Input.is_action_just_pressed("Jump") and is_on_floor():
	velocity.y = JUMP_VELOCITY

you can add this elif right after it, to create a short jump from short taps:

elif Input.is_action_just_released("Jump") and velocity.y < 0:
	velocity.y = JUMP_VELOCITY / 2

You can divide it by a larger number (for example, 4) to create an even smaller jump.

Note that if the value assigned to velocity.y is large enough (for example, dividing JUMP_VELOCITY by 2), it gives the player enough time to spam the jump button while in the air, causing the player to double/triple/infinite jump. To prevent this, we need to modify that elif block of code by only allowing one jump:

var _jumpCounter = 0

...

elif Input.is_action_just_released("Jump") and velocity.y < 0 and _jumpCounter == 0:
	velocity.y = JUMP_VELOCITY / 2
	_jumpCounter = 1
	
if is_on_floor():
	_jumpCounter = 0

Hope this helps!

1 Like

Hi, I think this code doesn’t take into account gravity. The player’s velocity.y will increase every frame because of gravity. So, if the player has a velocity.y of -0.1, they’re still technically traveling upward. And if the player releases the “jump” key at that moment, the velocity.y will actually decrease to JUMP_VELOCITY / 2 which will make it look like the player jumped again in midair. I would instead do something like this:

if Input.is_action_just_released("jump") and velocity.y < 0:
    velocity.y = 0

This means that if the player is traveling upwards and releases the jump key, the y-velocity will be set to 0, which cuts the jump short.

1 Like