Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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.     }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    1,867
    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:
    876
    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. }
     
    ROBYER1 and mkcuk like this.
  4. hayone1

    hayone1

    Joined:
    May 20, 2017
    Posts:
    4
    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:
    277
    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:
    35
    +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. }
     
    ROBYER1 likes this.
  8. ROBYER1

    ROBYER1

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

    aromana

    Joined:
    Nov 11, 2018
    Posts:
    35
    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:
    19
    +1 Still don't work on Android Device .The error make my app crash
     
  11. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,108
    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.
     
unityunity