RayCast2D not detecting collision even though debug view shows it

Godot Version

4.3

Question

In my game, the character moves between a limited set of navigation points that form an AStar2D graph. I my scene’s ready() function, I run a Script that figures out which points should be connected by using a RayCast2D.

As you can see in the screenshots below, my character starts in the bottom left hand corner and wants to move to the next navigation point (white square) to the right. The raycast is red, which I would think indicates that there is a collision, however the is_colliding() function returned false during the creation of the scene, so the character moves through the building.


Here’s what I already tried:

  • The TileMapLayer and the raycast both have the same collison layer/mask
  • The TileMapLayer’s collision is enabled
  • I’m calling force_update_transform() and force_raycast_update()
  • The raycast is set to collide with bodies, but not areas
    • When I set it to collide with bodies, it correctly detects collisions with the other navigation points
    • When I set it to not collide with areas, all arrows in the debug view are blue, and the player behaves the same
  • I added another check to my character to see if it works during physics processing, and it still fails to detect the collision

Alright, I figured it out. The solution is that large nodes like TileMapLayer need a second longer to truly be ready (that’s why other navigation points were correctly detected). A proper solution would be to not make any physics-related calls from the ready() function, but for now adding await get_tree().process_frame before the call also did the trick.

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