Jump Up
Part of the Input and Movement section of Coddy's Godot journey. Lesson 13 of 28.
To jump, set velocity.y to a big upward speed in the frame the player presses jump. Up is less y, so the number is negative:
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()You do not code the way down. Gravity keeps adding to velocity.y, so the hero slows as it rises, stops at the top and falls back.
Use is_action_just_pressed, not is_action_pressed. A held key would set the speed to -500 every frame, and the hero would fly off the screen.
Challenge
EasyWhen you press jump, set velocity.y to jump_velocity. The hero should jump up and land on the floor again. Holding the key must not make it fly.
Try it yourself
extends CharacterBody2D
var gravity = 1000
var jump_velocity = -500
func _physics_process(delta):
velocity.y += gravity * delta
move_and_slide()
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Input and Movement
5Project: Platformer Hero
Hero: RunHero: JumpPractice on your own: Godot playground