Godot Version
Godot 4
Question
I have a problem implementing double jump because method is_on_floor() gives true first frame after the jump has happened. Ex. I put print method in is_on_floor and jump and after the first jump, it prints one true. In my case that’s bad because character gets an extra jump out of it because I use is_on_floor to reset jump_count. I tried it with versions 4.4, 4.3 and 4.2 and they all have the same issue, but when I watch a tutorial all the codes work somehow. This is my movement code so far
func _physics_process(delta) → void:
# Get inputs
var horizontal_input := Input.get_axis(“move_left”, “move_right”)
var jump_attempted := Input.is_action_just_pressed(“jump”)
# Add the gravity and handle jumping
if jump_attempted or input_buffer.time_left > 0:
if coyote_jump_available: # If jumping on the ground
velocity.y = jump_velocity
coyote_jump_available = false
jump_count += 1
print("Jump 1: $", jump_count)
elif jump_attempted and jump_count < max_jumps: # Double jump
velocity.y = jump_velocity
jump_count += 1
print("Jump 2: $", jump_count)
elif jump_attempted: # Queue input buffer if jump was attempted
input_buffer.start()
# Shorten jump if jump key is released
if Input.is_action_just_released("jump") and velocity.y < 0:
velocity.y = jump_velocity / 4
# Dash is not on cooldown
if Input.is_action_just_pressed("dash") and dash_cooldown.time_left == 0:
dash_cooldown.start()
dash(last_input_direction)
# Apply gravity and reset coyote jump
if ray_is_on_floor():
print("true")
coyote_jump_available = true
coyote_timer.stop()
jump_count = 0
else:
if coyote_jump_available:
if coyote_timer.is_stopped():
coyote_timer.start()
velocity.y += get_my_gravity(horizontal_input) * delta
# Handle horizontal motion and friction
var floor_damping := 1.0 if ray_is_on_floor() else 0.2 # Set floor damping, friction is less when in air
if horizontal_input:
velocity.x = move_toward(velocity.x, horizontal_input * SPEED, ACCELERATION * delta)
last_input_direction = horizontal_input
else:
velocity.x = move_toward(velocity.x, 0, (FRICTION * delta) * floor_damping)
# Apply velocity
move_and_slide()