how to only jump when on floor instead of flying

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

my code

func _physics_process(delta):
velocity.y += gravity * delta
if Input.is_action_pressed(“ui_right”):
motion.x = 400
elif Input.is_action_pressed(“ui_left”):
motion.x = -400
elif Input.is_action_pressed(“jump”):
motion.y = -800
else:
motion.x = 0
motion.y = 0
gravity = 400
move_and_slide(motion)

:bust_in_silhouette: Reply From: p7f

You could try with:

elif Input.is_action_pressed("jump") and is_on_floor():
    motion.y = -800

Also, note that you should update the motion using move_and_slide so the gravity doesnt add while on floor, like this:

motion = move_and_slide(motion, Vector2.UP)

I also added the second parameter as i assume you are making a side scroller from your controlls. The function needs that parameter to know where is the floor.

thank you, i have been stuck on this for hours

fish | 2020-07-30 09:14

No problem. If it worked, you may select the answer so others see its solved!

p7f | 2020-07-30 11:59

I’ve been trying to get is_on_floor() to work for so long! Thank you!

CopperyMarrow15 | 2020-07-31 03:32