IsPointerOverGameObject doubt (from other project)

Hi Hugo.

I don’t know if you can solve these kinds of doubts here (and what do you need to see to solve it), if you can’t tell me and I ask you where you tell me.

In another project I’m making use of that same function but it doesn’t work as I want (well, I probably don’t use it as I should, je, je, je).

It is a 2D game for Android, I use the “Touches” to rotate the player but when I click on the “UI” to fill a bar, the player turns to that side.

I do not know if being a game for Android and using the “Touches” I have to do more things (validations) or not.

Thanks a lot!!!

Have you tested to see if that is the issue? IsPointerOverGameObject(); is returning false when you click on a UI button?
Check your Event System, do you have the Touch Input module? Can you interact with UI buttons?
Perhaps it’s due to how you’re capturing the touches for the player to rotate, how are you handling that?

1 Like

IsPointerOverGameObject() doesn’t work on Android, that’s why you are having issues. However, fixing it is outside of my ability.

1 Like

Did you add the Input module?

Alternatively you can manually do the raycast yourself with EventSystem.current.RaycastAll();

The problem with EventSystem.IsPointerOverGameObject() is that without a parameter, the system assumes “Is Mouse Over GameObject”. On Touch, you need to determine if there is a touch in the first place, and then call IsPointerOverGameObject with the TouchID. One might think this ID would be something simple like 0 or 1, but it’s actually a unique id that is assigned to each touch.

Using the OLD input system, you can determine IfPointerIsOverGameObject with this:

public static bool IsUITouched()
{
   foreach (Touch touch in Input.touches)
   {
       int id = touch.fingerId;
       if (EventSystem.current.IsPointerOverGameObject(id))
      {
          return true;
      }
   }
   return false;
}

In the New Input system, we have to get the id a bit differently:
first, make sure you are using:

using UnityEngine.InputSystem.EnhancedTouch;
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;

Then this function should tell you if it’s over a GameObject:

    public static bool IsUITouched()
    {
        foreach (var touch in Touch.activeTouches)
        {
            int id = touch.touchId;
            if (EventSystem.current.IsPointerOverGameObject(id))
            {
                return true;
            }
        }
        return false;
    }
2 Likes

Hello.

Sorry for the delay in response, end of vacation and start of school year.

Thank you very much for the clarifications, I think I had not taken into consideration the “ID” of the touch when asking for the method “IsPointerOverGameObject()”.

I will try it as soon as I can.

Thank you all very much again.

1 Like

Privacy & Terms