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. It can jump twice before it lands, never three times. Count jumps in a jumps_left variable that is set to 2 on the floor.

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