RayCast3D Intermittently Not Colliding

Godot Version

4.2.1

Question

Hi all.

I’m currently banging my head against a confounding issue in my code.

I’m working on ledge detection in order to create a climbing/mantling system in my FPS. My detection strategy at the moment involves a player that has has two cylindrical ShapeCasts and one downward RayCast. If the lower ShapeCast is colliding and the upper ShapeCast is not, then I get the collision point on the lower ShapeCast, move the RayCast’s X and Z position to that of the lower ShapeCast’s collision point, and get the collision point on the RayCast. That tells me where the ledge is on the object I’m attempting to climb.

Here is a screenshot of the in-game result, where I am generating a sphere at the point of RayCast collision.

This method is working ~95% of the time without issue, however the other ~5% of the time the RayCast will not collide after I move it to the collision point of the lower ShapeCast. Somehow the RayCast isn’t colliding even though the ShapeCast does.

if (
    not is_on_floor()
    and lower_shapecast.is_colliding()
    and not upper_shapecast.is_colliding()
):
    var side_point: Vector3 = lower_shapecast.get_collision_point(0)
    ledge_raycast.global_position = Vector3(side_point.x, ledge_raycast.global_position.y, side_point.z)
    ledge_raycast.force_raycast_update()
    if not ledge_raycast.is_colliding():
        # This happens ~5% of the time.
        print("raycast not colliding!")
    var top_point: Vector3 = ledge_raycast.get_collision_point()
    ledge_indicator.visible = true
    ledge_indicator.global_position = top_point

I should mention that I have been unable to identify specific situations that can guarantee the RayCast will not collide. It seems to happen randomly from my testing.

Have any of you experienced something similar, or know how I can resolve this without “hacky” (slightly adjusting the RayCast position until it collides) methods?

Many thanks in advance!

Hey hey! I just had a similar issue with my 2d game…

My issue came up because the movement of my sprite was lerping too far into the collision object. I could get the “5%” issue up to 100% by limiting Engine.max_fps.

I ended up having to extend the raycasts out further and lower the player movement speed (amongst other things) but hopefully this explains what might be happening.

@pillaged Thanks for your response!

I don’t believe this is the same issue as in my case, as limiting the FPS or the physics tick rate doesn’t increase the likelihood of my issue occurring.

I was running into the same issue when dealing with a wall running check. I think in my case it might have been due to cases where the raycast just barely touched the wall during the wall run, causing it to miss collisions.

My fix was to move the player closer to the wall when wall running, ensuring that the raycasts are guaranteed to intersect with the wall.

Could it be that your raycasts are located on the edges of the colliding geometry and not getting a clean intersection?