Search code examples
c#unity-game-enginevirtual-realityraycasting

How do I prevent certain objects from receiving the raycast?


I'm using the following code to perform a raycast and detect the GameObjects that are hit:

var eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = screenPosition;
var results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);

However, I want only certain objects to be hit by the EventSystem.current.RaycastAll. Is there any way to allow only certain GameObjects to receive the raycast?


Solution

  • Two ways, one you can separate each UI categories to different Canvases and raycast from different graphicRaycaster component every time your need changes.

    Or, I think the graphicRaycaster and EventSystem.current.RaycastAl both return an array of all UI objects hit, so why not just filter out manually by script based on their tags or so.