Menu
CoddyTech

Double Jump

Part of the Input and Movement section of Coddy's Godot journey. Lesson 17 of 28.

A double jump lets the hero jump once more in the air. is_on_floor() cannot allow that, so the hero counts its jumps.

Keep the count in a variable. Set it to 2 while the hero is on the floor, and subtract one for each jump:

var jumps_left = 0

func _physics_process(delta):
	velocity.y += gravity * delta
	if is_on_floor():
		jumps_left = 2
	if Input.is_action_just_pressed("jump") and jumps_left > 0:
		velocity.y = jump_velocity
		jumps_left -= 1

The first jump makes jumps_left 1, and the air jump makes it 0. A third press does nothing. Landing sets it back to 2.

challenge icon

Challenge

Medium

Give the hero a double jump.

  1. Add var jumps_left = 0 at the top, and set it to 2 while is_on_floor().
  2. Jump when jumps_left > 0 in place of is_on_floor(), and take one off jumps_left.

Press Play: the hero jumps twice before it lands, never three times. Then Check.

Try it yourself

extends CharacterBody2D

var speed = 220
var gravity = 1000
var jump_velocity = -500

func _physics_process(delta):
	velocity.y += gravity * delta
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_velocity
	if Input.is_action_just_released("jump") and velocity.y < 0:
		velocity.y = velocity.y / 2
	velocity.x = Input.get_axis("move_left", "move_right") * speed
	move_and_slide()
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Input and Movement

Practice on your own: Godot playground