Search Unity

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

Discussion in 'UGUI & TextMesh Pro' started by Jroel, May 26, 2019.

  1. Jroel

    Jroel

    Joined:
    Oct 25, 2013
    Posts:
    14
    Specs:
    Unity version: 2018 2.5f1 personal

    Scenario:
    My player is controlled by clicks on the screen in Update on a script added to the player:
    Code (CSharp):
    1. if(EventSystem.current.IsPointerOverGameObject()){
    2. Ray   ray  = Camera.main.ScreenPointToRay(Input.mousePosition);
    3. float dist = 0;
    4. new Plane(-Vector3.forward, transform.position.z).Raycast(ray, out dist);
    5. body.AddForce( (ray.GetPoint(dist) - transform.position) * 100f );
    6. }
    Next, I have a GUI Button setup on my Canvas with a listener in a script added to the GUI Button element:
    Code (CSharp):
    1.  
    2. m_BombButton.onClick.AddListener(ClickBomb);
    3. void ClickBomb()
    4. {
    5.         Debug.Log("m_BombButton pressed...");    
    6. }
    Desired Goal:
    My goal is to prevent the ScreenPointToRay from going through the GUI Button and acting as a click on the screen.

    Things I've tried:
    - Many different implementations of EventSystem.current.IsPointerOverGameObject()
    - OnPointerDown/Up with IPointerDown/UpHandler ( could get this to fire, but stopped working)
    - Using click on Sprite Images as wells as GUI Button. Neither seem to fire.
    - Legacy (pre 4.6) solutions


    Not sure if these functionalities work differently on 2018 than previous versions? But this is becoming a two week search with no solid results. Docs are better, but still do not describe anything beyond the basics. Google just keeps bringing me back to the same links.
     
    Last edited: May 26, 2019
  2. Jroel

    Jroel

    Joined:
    Oct 25, 2013
    Posts:
    14
    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:
    https://forum.unity.com/threads/ispointerovergameobject-returns-always-true.520060/
    Which then led me to:
    https://answers.unity.com/questions/1429689/specific-ui-element.html
    So thanks to Hellium for the code snippet

    Code (CSharp):
    1. private bool IsOverUI(){
    2.     bool isOverTaggedElement = false;
    3.         if (EventSystem.current.IsPointerOverGameObject() )
    4.         {
    5.             PointerEventData pointerData = new PointerEventData(EventSystem.current)
    6.             {
    7.                 pointerId = -1,
    8.             };
    9.  
    10.             pointerData.position = Input.mousePosition;
    11.  
    12.             List<RaycastResult> results = new List<RaycastResult>();
    13.             EventSystem.current.RaycastAll(pointerData, results);
    14.  
    15.             if (results.Count > 0)
    16.             {
    17.                 for( int i = 0 ; i < results.Count ; ++i )
    18.                 {
    19.                     if( results[i].gameObject.CompareTag( "Button" ) )
    20.                         isOverTaggedElement = true ;
    21.                 }
    22.             }
    23.         }
    24.         return isOverTaggedElement;
    25.      }
    26. }

    Unity... do better... this is bs
     
    ghellee likes this.
  3. ghellee

    ghellee

    Joined:
    Dec 6, 2017
    Posts:
    5
    Great answer! Thanks a lot!

    Don't forget to add Physics Raycaster to the camera!