Sensing if the player is close to the ground in a platformer

Godot Version

4.5.1 Linux

Question

I’m trying to sort of make my own version of is_on_floor() for a platformer project, where instead of needing to specifically be on the floor to jump, you just need to be a certain distance from it. I tried doing this with a downward raycast to start off, and it’s sort of worked but the raycast’s behavior is extremely janky and inconsistent. I can’t figure out what it’s doing.

I get the status of the raycast every physics tick like this:

if $MainCollision/RayCast2D.is_colliding():
	ground_close = true
else: 
	ground_close = false
$Control/Label3.text = "%s" % ground_close

I then use a label to monitor it. But at that point whatever it does is seemingly random. Sometimes it flickers between true and false every frame, or just stays as true forever, or returns true when there is very obviously nothing there. I have them set to ignore parent, and see every collision mask. The testing just happens on a static square collision platform with decent depth.

The raycasts are arranged like that under the player. For testing purposes, only the leftmost one is actually in use.

Any ideas on how these things work? Is there any other, better way to accomplish what I’m trying to do?

The only reasons I believe may affect your results are:

  • If the player’s y velocity is too fast and the player is already on the floor before the raycast could even notify that it has collided. In this case, you may need to increase the length of the raycast or reduce player velocity

  • You did not set the raycast to hit from inside

I suspect it’s one, but try fixing both.

Also, what specific mechanic are you trying to implement, maybe there is a better way to do it.

1 Like

Enable Debug > Visible Collision Shapes to see what your raycasts are doing at runtime.

For my player I am using combination of Raycasts and Area3D. * or Area2D in your case

  • Area3D to detect close to ground ( for Buffer jumping )

  • 2 Front and Back Raycasts for detecting edge of floor

  • 1 Middle one for placing Blob Shadow

The reason I didn’t use Raycast to detect close to ground is, as someone stated above, velocity is too fast for it to detect. Area seems to work better for something like this.