Double Jump Code Issue

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

Player just jumps once. Can someone tell me whats wrong?

var velocity = Vector2()
var on_ground = false
var has_double_jumped = false

if is_on_floor():
	on_ground = true
else:
	on_ground = false

if Input.is_action_pressed("ui_up"):
	if on_ground == true:
		velocity.y = JUMP_POWER 
		has_double_jumped = false
		on_ground = false
	
	elif on_ground == false and has_double_jumped == false:
		velocity.y = JUMP_POWER
		has_double_jumped = true

I know this is really, REALLY old, but when I put that code in, it says "Unexpected token: “if” by the “is_on_floor”. What can I do to fix this?

GabeCodes72 | 2021-04-13 20:41

:bust_in_silhouette: Reply From: usurun

Shouldn’t you set double_jumped to false when it’s on ground ?

Just tryed but still it does nothing.
Managed only to make it jump infinite times or just one.
Don´t understand what’s not working in my script.

But thanks!

pp09 | 2019-05-15 12:06

Same here. I’ve tried do this on diffrent tutorials and no one solve my problem :< Player still jump only one time or infinite if I toggle of “is on floor” script.

FigusKettuGames | 2020-09-09 15:21

:bust_in_silhouette: Reply From: Fenderbate

Just in case you haven’t figured it out yet, change the Input.is_action_pressed("ui_up")to Input.is_action_just_pressed("ui_up")

The reason it didn’t work is because Input.is_action_pressed("ui_up")checks if the button is being held down, thus when you press it, the first and second jump gets executed at the ame time, making it look like a single jump.

That fixed it thanks a lot!

pp09 | 2019-05-15 12:51

Oh, thanks! Now it’s works! ^w^

FigusKettuGames | 2020-09-09 15:21

Yes fixed it for me. Thanks a lot! :slight_smile:

scalev | 2021-03-21 16:51