In my game, the player can jump if the up button is pressed and they are on the ground. The velocity.y is also supposed to be set to 0 when on the ground, but when printing the velocity, velocity.y keeps jumping between 0 and whatever value velocity.y was before the player lands.

e.g.

The problem with this is that sometimes I'll press up while on the ground and the player won't jump, probably because at the moment i pressed up it was on the random value rather than 0.

my code:

thanks in advance

  • The velocity of 500 is coming from gravity (30000) * delta (1/60s).
    is_on_floor is true if the last move you did hit the floor. So when you hit it goes true and the velocity.y is set to 0.
    On the move_and_slide further down the function, the player isn't moving down (0 velocity.y) so it doesn't hit the floor, it's hovering.
    Then on the next _physics_process it sees that no floor hit happened, so it applies gravity to make the velocity 500 again.

    I'd say try moving the line where you add gravity out of the else: section and always do it (so there's always an attempt to move down, which will hit the floor).

The velocity of 500 is coming from gravity (30000) * delta (1/60s).
is_on_floor is true if the last move you did hit the floor. So when you hit it goes true and the velocity.y is set to 0.
On the move_and_slide further down the function, the player isn't moving down (0 velocity.y) so it doesn't hit the floor, it's hovering.
Then on the next _physics_process it sees that no floor hit happened, so it applies gravity to make the velocity 500 again.

I'd say try moving the line where you add gravity out of the else: section and always do it (so there's always an attempt to move down, which will hit the floor).

    Kojack worked perfectly, no problems now. Thanks for the help!