RayCast3D Intermittently Not Colliding

Godot Version



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