Does anyone know who to block a physics Raycast from passing through the UI?

In my project when I click on a UI button (screen space) the ray will be detected by the UI element AND on any objects behind it in world space. Example: clicking a move button automatically moves my agent to the position BEHIND the UI button. I wish it was as easy as just checking the raycast target box.

I have used EventSystems.OnPointerEnter and EventSytems.OnPointerExit to reveal and hide UI images and text but can’t quite put together a way to stop the ray when it hits the button.

Two separate rays? Perhaps one with a distance of 0 or 0.01? If so… How?

There are a lot of inconsistent hacks and poorly conceived work arounds for this; ranging from just won’t work to laugh out loud preposterous. What is consistent is that there is a whole crowd of people who are struggling with this issue.

Anybody want to show off some code magic?

PS. Code Magic is similar to Dance Magic but with less David Bowie.

You can use EventSystem.current.IsPointerOverGameObject() to check if the cursor is over a UI item. It will return true if the mouse pointer is over a UI element. You’d put this in your movement bits and ignore the click if it returns true


Goodness. I just checked the repo and I missed it RIGHT FREAKIN’ THERE… In UnitActionSystem.Update().

I just followed your advice and managed solve the issue myself; made a neat little all around function to handle this sort of thing only to find out that the problem was solved before it was a problem. It never was a problem. Heh, at least I learned something.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms