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?
public void SetTargetPosition()
{
Plane plane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float point = 0f;
Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit[] hits = Physics.RaycastAll(ray);
isMoving = false;
foreach (RaycastHit hit in hits)
{
if (EventSystem.current.IsPointerOverGameObject())
return;
if (plane.Raycast(ray, out point) && hit.collider.gameObject.tag == "Ground" )
{
targetPosition = ray.GetPoint(point);
isMoving = true;
}
break;
}
}
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.
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.
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 resultsList, and clearing it before the RaycastAll().
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.
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!
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-
Here is what I put on my UI class, following @ADNCG 's solution. I felt it would be more useful for me to sometimes know if a certain object is ātop-mostā in the UI, like having windows on top of windows. Using Unity 6000.0.32f1.
I just faced the same problem when I was trying to perform a raycast during the Ended phase of my input. It seems that IsPointerOverGameObject() does not work when used during the final states of an input (Ended or Canceled).
To solve this problem I cached and refreshed the result during the Began, Moved and Stationary states of my input and used this cached if needed. I can then acces the information through a dedicated method. Here is my code :
public bool IsMainInputOverUi()
{
if (Application.isEditor)
{
return EventSystem.current.IsPointerOverGameObject();
}
if (Input.touchCount <= 0)
{
return false;
}
var mainTouch = Input.GetTouch(0);
bool result;
if (mainTouch.phase == TouchPhase.Ended || mainTouch.phase == TouchPhase.Canceled)
{
result = _isOverUi;
}
else
{
result = EventSystem.current.IsPointerOverGameObject(mainTouch.fingerId);
}
return result;
}
Here _isOverUi is my local variable that is refreshed during the Began, Moved and Stationary states.