How to physics raycast through overlay UI?
I have a transparent image full screen inside an overlay canvas. The transparent image only turns visible on horizontal swipes (pagination style). I would like to interact with my world space 3D objects with mouse input, but the overlay canvas is blocking my raycast. Are there any methods to pass through the overlay canvas image?
RayscastAll returns 0 if the raycast target of the image is set to true. I have to have it true, because I am detecting the swipe gesture on it.
Thank you.
Answer by doublemax · Oct 19, 2016 at 10:47 PM
Put the overlay on a dedicated layer and ignore that layer in the RayCast call (layerMask parameter).
I have tried it, but doesn't seem to work. I've created a new Layer on the number 11 slot.
if(Physics.Raycast(ray, out hit, 100.0f, 11)) {}
This is how the raycast looks now.
The RayCast call expects a Layer$$anonymous$$ask, not a LayerNumber.
Corrected code:
Layer$$anonymous$$ask mask = Physics.DefaultRaycastLayers & ~ (1 << 11);
// alternatively:
Layer$$anonymous$$ask mask = Physics.DefaultRaycastLayers & ~Layer$$anonymous$$ask.Get$$anonymous$$ask("layername");
if(Physics.Raycast(ray, out hit, 100.0f, mask)) {}
I have put my Tooltip (3D object) layer on the 8th position and the overlay canvas elements to the 9th position. $$anonymous$$y code now looks the following:
if(Physics.Raycast(rayOrigin, out hitInfo, 100.0f, 1 << 8)) {}
But unfortunately it still doesn't work. Any idea what could be the issue?
However the RayCastAll now returns both the object I want to hit and the overlay canvas. I can take now the [1] from the hits array, so I can make it work. I still wonder why doesn't it work with the simple Raycast.