Avoiding GUI Elements on click when using Camera.main.ScreenPointToRay

I found 1 solution… though, I feel it is very unorthodox and too intensive to pass as a viable solution. But so does adding a script to every single gameobject in a scene just so you don’t click on them… ridiculous.

So here:

Which then led me to:

So thanks to Hellium for the code snippet

private bool IsOverUI(){
    bool isOverTaggedElement = false;
        if (EventSystem.current.IsPointerOverGameObject() )
        {
            PointerEventData pointerData = new PointerEventData(EventSystem.current)
            {
                pointerId = -1,
            };

            pointerData.position = Input.mousePosition;

            List<RaycastResult> results = new List<RaycastResult>();
            EventSystem.current.RaycastAll(pointerData, results);

            if (results.Count > 0)
            {
                for( int i = 0 ; i < results.Count ; ++i )
                {
                    if( results[i].gameObject.CompareTag( "Button" ) )
                        isOverTaggedElement = true ;
                }
            }
        }
        return isOverTaggedElement;
     }
}

Unity… do better… this is bs

1 Like