- Home /
Make UI element or button where a physics raycast can´t pass? Is that possible?
So, I´ve gone through a lot of different approaches, however I can´t seem to find an option to block a physics raycast on mobile to go through UI elements which is odd. I would imagine that this is a relatively common problem.
My approaches so far:
Uncheck this option, doesn´t seem to do anything for mobile (I did it on my buttons and on the panel that I have as background, neither of the two seemed to block the phyiscal raycast when I click on the button the object behind it is selected)
EventSystem.IsPointerOverGameObject
This works like a charm while I´m in the editor and use a mouse. Once the mouse is over the UI element I just prevent the raycast from going through it via script. Once I build it on mobile it doesn´t work at all since there is no mouse pointer.
I went on to test around with the event system to enforce the standard input module. I did read it would enable "IsPointerOverGameObject" on mobile.
I switched the "IsPointerOverGameObject" from 1 to -1 in all steps and tested it and none seemed to work, everytime I click my UI button the object behind the button is selected.
Is there ANY other option I could test. I´m literally running out of ideas and google searches, however I somehow doubt that this is not a more common issue. Or is it wrong to use "Physics raycast" for mouse movement and object selection?
I´m fairely new and this is the first bigger headache that I have regarding Unity. If anyone knows maybe something or has an idea please let me know.
Thanks in advance :)
Answer by Zaeran · Sep 19, 2020 at 02:17 PM
UI uses the GraphicRaycaster to test hits, not the Physics.Raycast.
Do a GraphicsRaycaster.Raycast first, and if 0 results are returned, then do your Physics.Raycast
Hey, thank you very much for your answer. I think your approach could also have worked, but I figured out another option which also did its job.
I use the event system of unity with currentSelectedGameObject which returns null if its not on an UI element which is interactable. So I just made a check for the click that it needs to be null to work and tested it on mobile and I can no longer click stuff behind buttons.
Sadly it doesn´t involve panels or other UI elements, only buttons as it seems, but when I want to block a raycast I just put an invisible button in the background which does nothing.
I´m not 100% sure if that is the smartest idea but it works.
I´ll still try to make your solution work, but I had some issues getting the raycast to start with. Need to look more into it, but for now I´ll mark it as solved
Your answer
Follow this Question
Related Questions
Indicating object outside camera screen view 0 Answers
move a object hit by raycast 2 Answers
How do I change the texture of a RawImage? 1 Answer
OnTriggerEnter and UI Collisions. 0 Answers
Unity 2D Car Side Scroller question. 1 Answer