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 from the floor.

  1. Add a second condition to the jump's if with and.
  2. Make it is_on_floor().

Press Play and press Space twice: the second press, in the air, does nothing. Then Check.

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()

All lessons in Input and Movement

Practice on your own: Godot playground