Multiple Raycasts with different Layermasks

I’m making use of multiple Raycasts with different LayerMasks for object detection and touch input.

I’m quickly realizing this can get out of control with Raycasts in mutliple locations in your project.

Is there a recommended procedure for handling multiple Raycasts?
Should they all be consolidated in a single class or?

I find that if I have a lot of classes that are interested in a “similar” raycast, for example a raycast at the mouse cursor position, I consolidate them into one “XXRaycaster” object. For example, I have a “StructureRaycaster” in my current game which is responsible for doing raycasts each frame at my mouse position and returning what structure or building in my game world the mouse is currently hovering over. The StructureRaycaster is a singleton in my scene, and it simply exposes a couple of events, like:

public delegate void StructureHoverHandler(StructureRaycaster source, IStructure structure);

public event StructureHoverHandler OnStructureHoverEntered;
public event StructureHoverHandler OnStructureHoverExited;

Then any of my other objects in the scene which care about what structure the mouse is currently hovering over simply subscribe to those events. For example I might have an object that sets a glowing outline on the current hovered structure, and another object that shows some UI related to the current structure, and so on.

1 Like

Thanks. Good strategy.