Jumping When I Release the Jump Button

Godot Version

v4.2.2.stable.official [15073afe3]

Question

Hello! I have followed this tutorial which covers holding the jump button to jump higher, and pressing the jump button lightly to do a small jump.
I’ve been wondering how I go about modifying this code and applying jump height when I let go of the jump button.
Basically, when I begin holding down the jump button, the eventual jump height increases over time and when I let go, that jump height is applied to the character.
If anyone has ever played Jump King, I am looking for something similar to that jumping mechanic, however without pressing arrow keys to jump in different directions like in the original game.

I’d recommend watching the only thirty second video to get a more in-depth display on how it works.

The player controller is a CharacterBody2D.

Here is the unmodified code, pasted from the tutorial video:

# Jump (taken from tutorial video)
const JUMP_VELOCITY_HIGH = -450
const JUMP_VELOCITY_LOW = -250
var HOLD_THRESHOLD = 0.3
var press_time = 0.0
var jump_height = JUMP_VELOCITY_LOW
var is_holding = false
var is_jumping = false
func _physics_process(delta):
	# Add the gravity.
		if global.paused == 0:				# this is not relevant to the tutorial
			if not is_on_floor():
				can_play_sfx_land = 1	# this is not relevant to the tutorial
				velocity.y += gravity * delta

			# Handle jump
			if Input.is_action_just_pressed("jump") and is_on_floor() and not global.dead == 1:
				_handle_bike_jump()
			
			elif Input.is_action_pressed("jump") and is_holding and is_jumping:
				_handle_bike_jump_hold(delta)
			
			elif Input.is_action_just_released("jump"):
				is_holding = false;
func _handle_bike_jump():
	get_node("particle_jump").emitting = false	# } these are not relevant to the tutorial
	get_node("particle_jump").emitting = true	# }
	velocity.y = JUMP_VELOCITY_LOW
	press_time = 0.0
	is_holding = true
	is_jumping = true
	sfx_jump.play()							# this is not relevant to the tutorial

func _handle_bike_jump_hold(delta):
	press_time += delta
	if press_time < HOLD_THRESHOLD:
		velocity.y = lerp(JUMP_VELOCITY_LOW, JUMP_VELOCITY_HIGH, press_time / HOLD_THRESHOLD)
	else:
		velocity.y = JUMP_VELOCITY_HIGH
		is_holding = false
		is_jumping = false

Any help will be much appreciated.

You could try something like this:

const MIN_JUMP_VELOCITY = -100
const MAX_JUMP_VELOCITY = -450
var HOLD_THRESHOLD = 1.0
var press_time = 0.0
var is_holding = false
var is_jumping = false

func _physics_process(delta):
	# Add the gravity.
		if global.paused == 0:				# this is not relevant to the tutorial
			if not is_on_floor():
				can_play_sfx_land = 1	# this is not relevant to the tutorial
				velocity.y += gravity * delta

			# Handle jump
			if Input.is_action_just_pressed("jump") and is_on_floor() and not global.dead == 1 and not is_holding:
				_handle_bike_jump()
			
			elif Input.is_action_pressed("jump") and is_holding:
				_handle_bike_jump_hold(delta)
			
			elif Input.is_action_just_released("jump"):
				is_holding = false;
                velocity.y = lerp(MIN_JUMP_VELOCITY, MAX_JUMP_VELOCITY, press_time / HOLD_THRESHOLD)

func _handle_bike_jump():
	press_time = 0.0
	is_holding = true

func _handle_bike_jump_hold(delta):
	if press_time < HOLD_THRESHOLD:
        press_time += delta

I’m sorry that i cannot verify if this code works at the moment, i’m not home while posting this, but the basic idea is:

  • If the player is on the ground and starts holding the jump button, start counting the time until the button is released, capped at max of HOLD_THRESHOLD seconds.

  • When the player releases the button, calculate the the jump velocity using lerp and set the player’s vertical velocity to the calculated amount

*Edit:
Just got home, tested the code, fixed a bug and verified that it now works. In my case i had to change “velocity.y” to “linear_velocity.y” but i don’t know if that’s because i’m on a different version of godot.

1 Like