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.
- After the jump's
if, add anifforInput.is_action_just_released("jump")andvelocity.y < 0. - Inside it, halve
velocity.y.
Press Play: a quick tap of Space hops, and holding it makes the full jump. Then Check.
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()
All lessons in Input and Movement
5Project: Platformer Hero
Hero: RunHero: JumpPractice on your own: Godot playground