Question about jump and land

Godot Version

4.2.2 stable

Question

What I want to achieve is, when the player jumps I want to change its horizontal move_speed, and when the player hits the ground, the horizontal speed will be changed back to a default value.

The question is that, when I do this:

velocity.y = -100
print("JUMP")
move_and_slide()

if is_on_floor():
    print("Hit the ground!!")

The output is always like this:

JUMP
Hit the ground!!!

The problem is the moment when a character takes off, the output if ‘is_on_floor’ is still true.
Thus, my horizontal speed will be changed back to default values immediately…

Thanks for reading my question, I’ll be extremely appreciated if you can help me.

Check if they are moving up

if is_on_floor() and velocity.y > 0:
    print("hit the ground!")

I think it’s because it instantly prints both of those, and at that instant it is indeed on the ground.

Try something like this maybe

if is_on_floor():
     horizontal_speed = default_value
else:
     horizontal_speed = airborne_value

if Input.is_action_just_pressed("JUMP"):
     if is_on_floor():
          velocity.y = JUMP_VELOCITY
          print("Hit the ground!!")
     else:
          print("airborne")

Thanks for your help, I changed my code according to your suggestion, and it now works fine in my project. I really appreciated! Hope you have a nice day!!

1 Like

This is also a solution to my situation, thanks you very much