Search Unity

making UI block raycasts

Discussion in 'UGUI & TextMesh Pro' started by Tom163, Oct 9, 2020.

  1. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    This should be a stupid question, or a checkbox somewhere, but...

    I want the UI block a raycast I send into the scene. I'm using the raycast in a point-and-click adventure like style, to identify objects on hover, click to walk somewhere, etc. I'd like that functionality disabled whenever I'm hovering over or clicking on a UI element.

    I've searched high and low and I understand that I could be using event system scripts, but I'd rather not - because that means I'd have to put those scripts on every freaking thing in the scene instead of handling the "what did I hit?" logic in a central place in the raycaster script.

    The other solution some youtube video offered was to put a thin 3d object behind the UI elements in the canvas. I don't think that's a clean solution to something that shouldn't even be a problem.

    I understand the technical problem most likely is that I'm raycasting from the camera position, which is in 3D space in the scene and never sees the UI elements. But heck, there's gotta be a standard way of doing that, every game in the world blocks UI clicks. So why is this not a default behaviour or a simple switch somewhere? What am I missing here?