How to create a short hop-like function for a 2D platformer?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CoatlessEskimo

Hello there! I’m a relative newbie to programming, and need some help understanding why my current idea isn’t working.

The goal is to create two different jump heights for the player in a 2D platformer.

Assuming the engine runs 60 frames per second, and func _physics_process(delta): runs every frame, I figured I could make a timer to determine for how many frames the player is holding down the jump button, and depending on whether or not the button is released within a certain window, change the jump height (kind of like in “Super Smash Bros.”)

I tried to accomplish this by setting a variable, jump_timer, to 0, and then saying that for every frame, the timer should increase by one if the jump button is being held.

if Input.is_action_pressed("ac_jump"):
	jump_timer = jump_timer + 1

After this, the game should check every frame to see whether or not the jump timer is less than to 4, or if it is greater than/equal to 4. Every time it checks to see whether it is less than 4, it should also check to see if the jump button has been released. If so, it will do motion.y = hop_force, which will jump to the short hop height. If the jump timer is above/equal to 4, then the game will do motion.y = jump_force, which should cause a full height jump. Afterwards, it does jump_timer = 0 to reset the frame count.

if is_on_floor():
	jump_refresh = 1
	if Input.is_action_pressed("ac_jump"):
		jump_timer = jump_timer + 1
		if jump_timer < 5:
			if Input.is_action_just_released("ac_jump"):
				motion.y = hop_force
				jump_timer = 0
			elif jump_timer >= 5:
				motion.y = jump_force
				jump_timer = 0

What ends up happening is that the full hop works, but if I release the jump button before the counter goes past 4, nothing happens at all. Can anyone tell me why this does not function so I can fix it?

var jump_force = -400
var hop_force = -200

:bust_in_silhouette: Reply From: jgodfrey

Completely untested, but based on your description, the below should be about what you want. Though, I’d be surprised if you can press and release a button with < 5 physics frames. That’d be just 0.066 seconds…

# If the jump button is pressed, start counting frames
if Input.is_action_pressed("ac_jump"):
    jump_timer += 1

# If the jump button was just released, see how high we should jump
if Input.is_action_just_released("ac_jump"):
    if jump_timer < 5:
        motion.y = hop_force
    else:
        motion.y = jump_force
    jump_timer = 0

The largest potential issue I can see there, is that I want the character to jump if the counter goes past 4 frames, even if the button is still being held down. Thank you for the help though!

CoatlessEskimo | 2020-03-09 19:06

Ah, I guess I missed that in your description. Again untested, but this should be close…

if Input.is_action_pressed("ac_jump"):
    jump_timer += 1
    if jump_timer > 4:
        motion.y = jump_force
        jump_timer = 0

if Input.is_action_just_released("ac_jump"):
    if jump_timer < 5:
        motion.y = hop_force
        jump_timer = 0

jgodfrey | 2020-03-09 19:23