![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
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