Menu
CoddyTech

Only From the Floor

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

The last lesson's hero can jump again in the air, as often as you like. It should jump only when it stands on something.

is_on_floor() is true when the last move_and_slide() left the body on a floor. In the air it is false.

and joins two conditions. The if runs only when both are true:

if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = jump_velocity
challenge icon

Challenge

Easy

Let the hero jump only while it stands on the floor. Pressing jump in the air should do nothing.

Try it yourself

extends CharacterBody2D

var gravity = 1000
var jump_velocity = -500

func _physics_process(delta):
	velocity.y += gravity * delta
	if Input.is_action_just_pressed("jump"):
		velocity.y = jump_velocity
	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