Hello,
I am using a Physics.Raycast() with terrain layer. But sometimes in game a Dialog appears with UI elements (and UI layer) and both UI buttons and raycasts work. I want to “turn off” the Raycasting, as this Dialog is shown.
Is there any nice way to do it?
What have I tried:
- Global static bool to know, when the Dialog is up - terrible, as dialog needs to set it true/false and if some other custom UI pops on camera, it needs to do the same (maybe semaphore then, no bool, but it’s still terrible).
- Check for UI plus Terrain layer masks in raycasts - terrible, generates boilerplate code and needs to join layer masks and then differentiate them again (and even doesn’t work, I don’t know, but it looks like the raycast hits terrain first using
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition)
) - Don’t use layer masks at all - terrible, as Raycasting shall be adjusted to be used with layer masks.
- Running raycast function twice - as bad as it sounds, as it is called in Update.
- Graphic Raycaster on Dialog with ‘Blocking Objects’ sets to ‘All’ makes no difference in the outcome.
- Event system is telling me, I am clicking solely on my Dialog.
My original code is simply this:
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, terrainShiftedLayerMask))
{
return hit.point;
}
Code from my second point is this:
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, terrainAndUIShiftedLayerMask) &&
hit.collider.gameObject.layer != uiLayerMask) // If we hit UI, skip
{
return hit.point;
}