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
EasyMake the hero jump.
- Between the gravity line and
move_and_slide(), add anifforInput.is_action_just_pressed("jump"). - Inside it, set
velocity.ytojump_velocity.
Press Play and press Space: the hero jumps and lands again, and holding Space does not make it fly. Then Check.
Try it yourself
extends CharacterBody2D
var gravity = 1000
var jump_velocity = -500
func _physics_process(delta):
velocity.y += gravity * delta
move_and_slide()
All lessons in Input and Movement
5Project: Platformer Hero
Hero: RunHero: JumpPractice on your own: Godot playground