Help reduce gravity on character2d when jump button pressed in the air

Godot Version


Hi, I’m new to godot and game dev in general and I’m making a Kirby clone. I want to add a feature where if a player holds space while in the air, they fall much slower than they would normally. So far, I can only get the character to fall up, for some reason (they just fly straight up) Here’s the code

if not is_on_floor():
velocity.y += gravity * delta
if Input.is_action_pressed(“float”):
gravity = 1


Any help would be appreciated

If this is in a process loop I think you got it. Just make the gravity negative.

To make it happen on the same frame you should modify gravity before applying it to the character velocity.

You could also add an else statement somewhere to change the gravity back to default. Either when float is not pressed or after the character is on the floor.

1 Like

Thanks for your reply. I tried all that, but the character doesn’t really float, it just flies up instead fall slower. However, when I do press float when they’re falling(second part of the jump) they do fall slower and it works as intended, but it’s not consistent enough to be a good mechanism

I think the problem is that when the character jumps and I press the button, it changes the gravity but the character still has that momentum from the jump, and there’s not enough gravity to stop that momentum, so they just keep going up. Idk how to fix this myself though

Okay, so I added the else statement and it seems to be working now, but it still goes way higher than it’s supposed to since it Carries the initial jump momentum. I just want the character to fall slower(which it does) not jump higher as well

Ah, okay we should cancel the upward velocity. Or we could do a check like

if velocity.y < 0.0 : # if falling use float speed else use normal gravity still.

(I could have the comparison wrong.)

Probably need to know what Kirby does in this situation. I assume the jump is cancelled, if so you could do that with this check. If float is pressed and you have upward momentum, set velocity.y to zero. Then start using float gravity.

1 Like

TYSM, it works now :smiley:
Really appreciate your help

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