Menu
CoddyTech

Hero: Jump

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

The pit is 80 pixels wide. Without a jump the hero cannot cross it. Add a jump_velocity variable and a jump that works only from the floor:

var jump_velocity = -500

# in _physics_process, after gravity:
if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = jump_velocity

A jump lasts about one second. In that time the hero runs 220 pixels, far more than the pit. Run at the pit and jump just before the edge.

challenge icon

Challenge

Easy

Let the hero jump with jump, only from the floor, with a jump_velocity of -500. Then jump over the pit.

Try it yourself

extends CharacterBody2D

var speed = 220
var gravity = 1000

func _physics_process(delta):
	velocity.y += gravity * delta
	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