Method is_on_floor is true first frame after jump

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()

This is not a engine bug, that’s a problem with your checking order, the character only moves after the move_and_slide call, so before that the character will stay in the same position when you give the jump input and when the code check for reset the jump, the character didn’t moved a inch because move_and_slide wasn’t called yet. You can solve that either moving the reset jump code above the jumping code or checking if the velocity.y >= 0 together with is_on_floor.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.