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.