I am in the process of converting a 2d scene that used the “old” method of raycasting through all objects and using broadcast to those objects.
- I have added two new layers to my scene: “Navigation” and “ClickableObjects”
- I have replaced the “old” raycasting/broadcast function on the main (and only) camera with a Physics 2D raycaster. the physics 2d raycaster is set to broadcast to only the “Navigation” and “ClickableObjects” layers.
- I have a NAVMESH game object that has a polygon collider 2d which illustrates the “clickable walkable area” of the scene. It has a script so that, if clicked, the event system “Pointer Click” calls a function on the navmesh script on the units to move to that clicked location. The navmesh is in the “Navigation” layer
- i also have clickable units throughout the scene. The idea is that, when these units are clicked, the will engage a chat window for the player to interact with. These units are in the “ClickableObjects” layer.
At this point, the navigation works as expected. The new event system sends a raycast to the NAVMESH which in turn calls the player units to move accordingly. This all works great.
However, the clickable objects no longer respond to clicks. In fact, the debug is never called on them that they registered a click. Now, if I walk over to them, disable the NAVMESH Physics 2d collider, and then click on the clickable objects, the chat window opens as expected. Also, if I move the clickable objects so that they are not “overlapping” the navmesh, everything works fine together.
I have tried the following to fix the problem:
- adjusting the clickable units to be “closer to camera.main” on the z axis. No effect.
- tried rearranging the layers to be in different order. No effect. Same issue.
- tried creating separate physics 2d raycasters on the same camera to cast to specific layers. No effect.
- tried creatiing a second camera with a raycaster casted to only one layer on each camera. no effect.
It seems that the NAVMESH polygon is absorbing the raycasts regardless of which layer it’s configured to. Is there any way to cast a 2d ray to multiple, overlapping objects in the same layer without having one absorb the cast?