how to double jump

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

i dont know how to double jump using my specific code

here is the code i have

extends KinematicBody2d

var velocity = Vector2.ZERO
const SPEED = 210
const GRAVITY = 35
const JUMPFORCE = -1100
func _physics_process(delta):
if Input.is_action_pressed(“right”):
velocity.x = SPEED
$AnimatedSprite.play(“walk”)
$AnimatedSprite.flip_h = false
elif Input.is_action_pressed(“left”):
velocity.x = -SPEED
$AnimatedSprite.play(“walk”)
$AnimatedSprite.flip_h = false
else:
$AnimatedSprite.play(“idle”)

if not is_on_floor():
	$AnimatedSprite.play("jump")
	
velocity.y = velocity.y + GRAVITY
if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = JUMPFORCE
	$SoundJump.play()

velocity = move_and_slide(velocity,Vector2.UP)
velocity.x = lerp(velocity.x,0,0.1)

func _on_fallzone_body_entered(body):
get_tree().change_scene(“res://level one.tscn”)

:bust_in_silhouette: Reply From: BoxyLlama

One option would be to add a boolean. Something like, can_doublejump. Then, you could add an additional check for the double jump that checks that they are both in the air and that can_doublejump is true. Then, set can_doublejump to false, only resetting it when they jump again from the ground.

if Input.is_action_just_pressed("jump") and is_on_floor():
  can_doublejump = true  // Reset can_doublejump to true
  velocity.y = JUMPFORCE
  $SoundJump.play()
elif Input.is_action_just_pressed("jump") and !is_on_floor() and can_doublejump:
  can_doublejump = false  
  velocity.y = JUMPFORCE
  $SoundJump.play()

A couple double jump tutorials:
How to do double jumps and flip the character by GDQuest on YouTube
Double Jump in Godot by A Creates on Youtube

3 Likes