About double jump

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

I’m creating a state machine for my player with the current states atm:

  • Idle;
  • Running;
  • Jump;
  • Falling;

All the states works fine, but now I want to implement a double jump, so my question is:
do I need to apply the “jump force” again before or after the move_and_slide?
Like that:

velocity = move_and_slide(velocity, Vector2.UP)
If Input.is_action_just_pressed("jump") and jumps < 1:
   jumps += 1
   init_jump()

Here’s the init_jump:

func init_jump():
	anim = ""
	state = states.JUMP
	velocity.y = jump_speed  # -300
:bust_in_silhouette: Reply From: ArthurER

it doesn’t matter it could just be in with the get input function packed in one test for jump button event it doesn’t need to be in two places. I expect it would not work reliably and if the button was not pressed quickly enough it would just jump twice as high. if I was making a double jump the second jump would test velocity.y and not allow the second jump until velocity.y hit a certain point, your jump speed reads as - 300 so when you jump velocity.y goes to - 300 then starts climbing back to zero so I would test if jumps == 0 then test if jumps == 1 and velocity.y > -100 , then tweak it from there. would also need to be a Boolean to say the player jumped to test when player is_on_floor after a jump to reset the jumps variable the way yours is if you jump once the variable becomes 1 then if you jump it becomes 2 and you cant double jump, your snip shows no reset of the jumps variable

on jump button event jumping = true
if jumping and is_on_floor(): jumping = false, jumps = 0