Variable Jump
Part of the Input and Movement section of Coddy's Godot journey. Lesson 16 of 28.
In most platformers, a tap makes a small hop and a held button makes a full jump.
To do this, cut the jump when the button is released. Input.is_action_just_released("jump") is true in the one frame the key comes up. If the hero is still rising then, halve its upward speed:
if Input.is_action_just_released("jump") and velocity.y < 0:
velocity.y = velocity.y / 2velocity.y < 0 means the hero is still rising. Without it, a release on the way down would slow the fall.
Hold the key past the top of the jump, and nothing is cut.
Challenge
MediumMake a short tap of jump a small hop. When the key is released while the hero is still rising, halve velocity.y. Holding the key still makes the full jump.
Try it yourself
extends CharacterBody2D
var speed = 220
var gravity = 1000
var jump_velocity = -500
func _physics_process(delta):
velocity.y += gravity * delta
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_velocity
velocity.x = Input.get_axis("move_left", "move_right") * speed
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