Why won't godot detect that my character is on the floor

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

I’m new to the engine and can’t tell what’s wrong with my code. Every time I run the game, it lets me jump once and then won’t let me jump again. The problem is with my CoyoteTime variable. The code is supposed to set it to true whenever you’re on the ground, but for some reason, it doesn’t reset. Here is all the code I have:

extends CharacterBody2D

const SPEED = 175.0
const JUMP_VELOCITY = -220.0

var gravity = 620
var CoyoteTime = true

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta

# Jumps and turns down gravity
if Input.is_action_just_pressed("ui_accept") and CoyoteTime == true:
	velocity.y = JUMP_VELOCITY
	gravity = 400
	CoyoteTime = false

Turns Gravity back up when jump is released

if Input.is_action_just_released("ui_accept"):
	gravity = 600
# Get the input direction and handle the movement/deceleration.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
	velocity.x = direction * SPEED
	velocity.x = move_toward(velocity.x, 0, SPEED)


func _on_jump_timer_timeout():
gravity = 620

if is_on_floor():
	CoyoteTime = true

if not is_on_floor() and velocity.y > 0:

func _on_coyote_timer_timeout():
CoyoteTime = false

1 Like
:bust_in_silhouette: Reply From: rhennig

Ok, to start, check in the inspector if, at your CharacterBody2D, the “Motion Mode” is set to grounded and the “up direction” in y is -1.

Yeah, both of those are set like that. Anything else I can do?

Luke Fenstad | 2023-06-21 17:33

Ok, so probably your timer is smaller than the time of your jump.

So the “_on_jump_timer_timeout()” function fires with the player on air and so “CoyoteTime” will never become true again…

Try to decrease:

const JUMP_VELOCITY = -220.0

to see if it fixes your problem.

rhennig | 2023-06-21 18:13

1 Like
:bust_in_silhouette: Reply From: godot_dev_

Given that @rhennig’s asnwer didn’t solve your problem and the fact you can’t jump twice means CoyoteTime is never being set to true, I can see the following reasons being the issue:

  1. You haven’t properly connected your oncoyotetimertimeout function to the timeout signal of your CoyoteTimer, so when the timer expires, your function is never called to reset CoyoteTime
  2. You have in fact properly connected the signal in the point I made above, but your player is in the air at the time of the timeout. This means is_on_floor() will be false and CoyoteTime = true is never called.
  3. If point 1 and 2 above don’t apply. This point may be the issue. Your is_on_floor() call is never true, because you are handling the velocity + move_and_slide() properly. This may be cause by the
    if not is_on_floor():
        velocity.y += gravity * delta

call in your _physics_process function. I beleive that gravity should always be getting applied, even if you are on the floor. If you are on the floor and gravity is not being applied, you are in fact not colliding with the floor since you aren’t accelerating into the floor.

  • Try removing if not is_on_floor(): from your _physics_process, to always apply gravity.

I didn’t mean to put the on the floor function inside the timer function. Thanks for helping me find that. What I want to happen is the CoyoteTime variable to be set to true anytime you’re on the floor. Any chance you could help me with that?

Luke Fenstad | 2023-06-21 21:51

I think just adding

if is_on_floor():

to the _physics_process function would do the trick

godot_dev_ | 2023-06-21 22:51

It works perfectly. Thank you so much!

Luke Fenstad | 2023-06-21 23:12