When I click on my UI, my physics raycast also hits objects behind the UI.
I’ve tried this nifty piece of code:
if (!EventSystem.current.IsPointerOverGameObject(-1))
(with and without the “-1”).
The effects of which are to block all ray casts, anywhere on screen (not only on UI). I’m using panels to organize my UI elements. I’ve tried ticking all panels and UI elements off in the inspector while in play mode to see if any of them are specifically blocking ALL the ray cast, but even with every panel and UI elements off, all ray casts are blocked.
I’ve also tried adding canvasgroups (with block raycast ticked on), but these don’t block physics raycasts…
I’ve been reading a lot of posts on the subject, but haven’t found anything on this problem specifically…
if (Input.GetMouseButtonDown (0)) { // only if the mouse was NOT clicked over a UI element if (!EventSystem.current.IsPointerOverGameObject(-1)) { if (Physics.Raycast (ray, out hit)) { if (hit.transform.gameObject.tag == "theThingToHit") { DoSomething(); } } }