Search Unity

IsPointerOverGameObject is not working on Mobile

Discussion in 'Scripting' started by mkcuk, Dec 23, 2019.

  1. mkcuk

    mkcuk

    Joined:
    Oct 11, 2019
    Posts:
    11
    Hello guys, I m trying to stop gameobject when send ray at UI/Panel front of ground.
    This part of code is working on PC well but in mobile its not working why?

    Code (CSharp):
    1.   public void SetTargetPosition()
    2.     {
    3.         Plane plane = new Plane(Vector3.up, transform.position);
    4.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    5.         float point = 0f;
    6.  
    7.         Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
    8.         RaycastHit[] hits = Physics.RaycastAll(ray);
    9.  
    10.         isMoving = false;
    11.         foreach (RaycastHit hit in hits)
    12.         {
    13.             if (EventSystem.current.IsPointerOverGameObject())
    14.                 return;
    15.  
    16.             if (plane.Raycast(ray, out point) && hit.collider.gameObject.tag == "Ground" )
    17.             {
    18.                 targetPosition = ray.GetPoint(point);
    19.                 isMoving = true;
    20.             }
    21.             break;
    22.         }
    23.        
    24.  
    25.     }
     
    Eristen likes this.
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Havent yet build a mobile app, but i'm pretty sure that IsPointerOverGameObject() is a function that makes no sense with touch-input. How would you hover over something with touches? Imagine touches as 'clicked' mouse inputs.
    I believe there are also specific Touch helper objects in Unity.
     
    mkcuk and Joe-Censored like this.
  3. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    I've used this snippet in the past to get the deed done. It'll break for multi-touch but it's easy to fix. I'm pretty sure you can just iterate over all positions without any problem, should you need multitouch support.
    Code (CSharp):
    1. private bool IsPointerOverUIObject()
    2. {
    3.     PointerEventData eventData = new PointerEventData(eventSystem);
    4.     eventData.position = Input.mousePosition;
    5.     List<RaycastResult> results = new List<RaycastResult>();
    6.     eventSystem.RaycastAll(eventData, results);
    7.     return results.Count > 0;
    8. }
     
  4. hayone1

    hayone1

    Joined:
    May 20, 2017
    Posts:
    8
    According to the new unity input module docs here[https://docs.unity3d.com/Packages/c....InputSystem.UI.InputSystemUIInputModule.html], a touch on the screen should he able to alter the value of that method. It unfortunately doesn't seem to work simply as the docs stated it would.
     
    Harinezumi likes this.
  5. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    Any updates on this as it's completely breaking my projects if I can't mask out UI elements when moving items on the screen on mobile
     
  6. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    What a surprise, the Unity docs says it works, but in reality, it doesn't. I just hope they really do work more on bugs for Unity 2021, because 2020.x is just unusable.

    On the other hand, I've just tried ADNCG's approach, and it works. To make it more memory efficient, I recommend storing the I recommend caching the results List, and clearing it before the RaycastAll().
     
  7. aromana

    aromana

    Joined:
    Nov 11, 2018
    Posts:
    137
    +1 just stumbled upon this too. I definitely think this is a bug, and I wasted several hours trying to solve this until I found ADNCG's solution.

    Here's the version I came up with that works with New Input system:


    Code (CSharp):
    1. private bool IsPointerOverUIObject()
    2. {
    3.     var touchPosition = Touchscreen.current.position.ReadValue();
    4.     var eventData = new PointerEventData(EventSystem.current) {position = touchPosition};
    5.     var results = new List<RaycastResult>();
    6.     EventSystem.current.RaycastAll(eventData, results);
    7.     return results.Count > 0;
    8. }
     
  8. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    Has anyone reported this as a bug?
     
  9. aromana

    aromana

    Joined:
    Nov 11, 2018
    Posts:
    137
    I didn’t, I was able to work around this entirely by using an empty text input as a touch capture region. Would still be good to report it though!
     
  10. i_cassell

    i_cassell

    Joined:
    Apr 6, 2019
    Posts:
    21
    +1 Still don't work on Android Device .The error make my app crash
     
  11. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    In my case actually it was not a bug or issue, if you call it outside of update it can be flakey, only use it in update I found - I was using a bool function to check something in update, so the bool function was running after the tap in update initiated it and it worked most of the time but sometimes that function would run just after the tap so of course the pointer was not over UI.

    I would encourage others to do the same and carefully observe when their code is running around the time of the tap - sometimes like me you may just miss the tap on screen and the result will be false.
     
  12. gabagpereira

    gabagpereira

    Joined:
    Jan 21, 2019
    Posts:
    24
    Same issue! I've debugged on screen the result of IsPointerOverGameObject and apparently it only becomes true when dragging the finger over a UI element, but never when tapping/entering. Even if I pass the TouchID as a parameter the issue still persists. On the other hand, it works fine with the mouse on PC, though.

    Shame on the staff behind the new Input System who apparently isn't able to provide a stable package for all platforms, even though it is out of preview!
     
    martin-schoen and Ragueel like this.
  13. Fredjack99

    Fredjack99

    Joined:
    Aug 4, 2018
    Posts:
    2
    Not sure if this is related to your problem, but I found that checking IsPointerOverGameObject for mouse OR touch based on the related event was unreliable. But when I started checking BOTH for every type of click then it started working as expected. I suspect that there is something going on with mouse to touch emulation that is messing this up.
    -Fred-
     
  14. doublehitgames

    doublehitgames

    Joined:
    Mar 5, 2016
    Posts:
    97
    I not understud.
    Same prolem here with unity 2022.
     
    bravophantom likes this.
  15. lilwoz297

    lilwoz297

    Joined:
    Dec 5, 2018
    Posts:
    2
    Wow I can't believe @Fredjack99's fix of "checking BOTH for every type of click" worked, but that's what did it for me!