- Home /
Converting "old" raycasting method in 2d scene to new event system, now raycast is being "absorbed" by specific layer
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?
Answer by MoschPitt · Jul 01, 2015 at 10:31 AM
After 6 hours of trying different things, this was the solution that solved it: - On my navmesh gameobject (which consist of a polygon collider, an event trigger and a script to tell the units to "move here"), I put a sprite renderer. - I did not put a sprite on it--instead i simply used it to set the navmesh's sprite layer to the "ClickableObjects" layer and set the sort order to -9999 - Now the units are "in front of" the floor for a raycast and respond to clicks - When the player clicks anywhere there is not a unit, the floor catches the raycast
it's a pretty basic solution, though it seems a bit sloppy that I have to create an invisible sprite renderer on every clickable invisible area in the game just to control the ray cast priority, especially when those objects already have the appropriate 2d colliders and event triggers. Either way, I hope someone finds this helpful as it was the missing link to update my pre-Unity-4.6 game into the new event system and it works great!