Why is this raycast still hitting ground below a UI button, even after I pass a check to see if fing

I’ve been reading lots and trying many variations of this method to fix my issue, but cannot quite seem to understand it all. The “…IsPointerOverGameObject(t.fingerid)” part is supposed to stop the touch being counted in my update method (so that it doesnt call the related functions in my ‘Goodguys’ array.)

The actual result i want is that a touch just to the screen will set the destination of the players NavMesh, then there is a UI button with Events Trigger on it, if the player holds that button in, then also presses somewhere on the screen, that will make the Goodguys shoot their guns.

It all works fine if you dont press the Shoot button. But if you do, the debug.log still states that “object touched is Ground” and the players (/or Goodguys) still just turn around, they will shoot, so know the method is having some effect, but their target underneath the UI button, and AFAIK my code should be stopping that from happening already.

Anyone kind enough so spot what I have done wrong?? Many thanks :slight_smile:

public class TouchHandler : MonoBehaviour //, IPointerClickHandler
{
RaycastHit hit;
GameObject objectBeingTouched;

Camera cam;
GameObject[ ] goodguys;

private void Awake()
{
cam = GameObject.FindGameObjectWithTag(“MainCamera”).GetComponent();
}

void Update()
{
if (Input.touchCount >= 0)
{
goodguys = GameObject.FindGameObjectsWithTag(“Goodguy”);

foreach (Touch t in Input.touches)
{
if (!EventSystem.current.IsPointerOverGameObject(t.fingerId))
{
Ray ray = cam.ScreenPointToRay(t.position);

if (Physics.Raycast(ray, out hit))
{
objectBeingTouched = hit.transform.gameObject;

if (t.phase == TouchPhase.Began)
{
//Debug.Log("BEHIND BUTTON TRUE OR FALSE? = " + EventSystem.current.IsPointerOverGameObject(t.fingerId));

Debug.Log("FINGER PRESSED = " + t.fingerId);
Debug.Log("BEHIND BUTTON TRUE OR FALSE? = " + EventSystem.current.IsPointerOverGameObject(t.fingerId));
foreach (GameObject goodguy in goodguys)
{
goodguy.SendMessage(“OnTouchStart”, hit, SendMessageOptions.DontRequireReceiver);
}

}
if (t.phase == TouchPhase.Moved)
{

foreach (GameObject goodguy in goodguys)
{
goodguy.SendMessage(“OnTouchMoved”, hit, SendMessageOptions.DontRequireReceiver);
}

}
if (t.phase == TouchPhase.Stationary)
{

foreach (GameObject goodguy in goodguys)
{
goodguy.SendMessage(“OnTouchStill”, hit, SendMessageOptions.DontRequireReceiver);
}

}
if (t.phase == TouchPhase.Ended)
{

Debug.Log("FINGER PRESS ENDED = " + t.fingerId);
Debug.Log("BEHIND BUTTON TRUE OR FALSE (ended)? = " + EventSystem.current.IsPointerOverGameObject(t.fingerId));
foreach (GameObject goodguy in goodguys)
{

goodguy.SendMessage(“OnTouchEnded”, hit, SendMessageOptions.DontRequireReceiver);
}

}
if (t.phase == TouchPhase.Canceled)
{

foreach (GameObject goodguy in goodguys)
{
goodguy.SendMessage(“OnTouchCancelled”, hit, SendMessageOptions.DontRequireReceiver);
}

}
}

}
}

}
}

// UI button toggles:
public void UiButtonShootModeToggle()
{
if (goodguys != null)
{
foreach (GameObject goodguy in goodguys)
{
Goodguy gg = goodguy.GetComponent();
if (gg.currentControlMode != Goodguy.ControlMode.SHOOT)
{
gg.currentControlMode = Goodguy.ControlMode.SHOOT;
}
else
{
gg.currentControlMode = Goodguy.ControlMode.MOVE;
}
}
}
}

//public void OnPointerClick(PointerEventData eventData)
//{

//}
}

(apologise i couldnt find a way to format the code in the question editor)

thanks. i will use them from now on. Dont suppose you can look at my code? here is what the update method currently looks like . I’ve tried dozens of different ways now:

    void Update()
    {
        if (Input.touchCount >= 0)
        {
           
            goodguys = GameObject.FindGameObjectsWithTag("Goodguy");

            for (int i = 0; i < Input.touchCount; i++)
            //foreach (Touch t in Input.touches)
            {
                Ray ray = cam.ScreenPointToRay(Input.GetTouch(i).position);
                if (Physics.Raycast(ray, out hit))
                {
                    if (Input.GetTouch(i).phase == TouchPhase.Began)
                    {
                        if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(i).fingerId))
                        {
                            // hit UI , so ignore
                            Debug.Log("THIS SHOULD ONLY BE CALLED IF FINGER IS NOT OVER UI !!!!!!!!!!!");


                            Debug.Log("FINGER BEGAN = " + Input.GetTouch(i).fingerId);
                            Debug.Log("BEHIND BUTTON TRUE OR FALSE? = " + EventSystem.current.IsPointerOverGameObject(Input.GetTouch(i).fingerId));
                            foreach (GameObject goodguy in goodguys)
                            {
                                goodguy.SendMessage("OnTouchStart", hit, SendMessageOptions.DontRequireReceiver);
                            }
                        }

                    }
                    if (Input.GetTouch(i).phase == TouchPhase.Moved)
                    {
                        foreach (GameObject goodguy in goodguys)
                        {
                            goodguy.SendMessage("OnTouchMoved", hit, SendMessageOptions.DontRequireReceiver);
                        }


                    }
                    if (Input.GetTouch(i).phase == TouchPhase.Stationary)
                    {

                        foreach (GameObject goodguy in goodguys)
                        {
                            goodguy.SendMessage("OnTouchStill", hit, SendMessageOptions.DontRequireReceiver);
                        }


                    }
                    if (Input.GetTouch(i).phase == TouchPhase.Ended)
                    {
                        Debug.Log("FINGER PRESS ENDED = " + Input.GetTouch(i).fingerId);
                        Debug.Log("BEHIND BUTTON TRUE OR FALSE (ended)? = " + EventSystem.current.IsPointerOverGameObject(Input.GetTouch(i).fingerId));
                        foreach (GameObject goodguy in goodguys)
                        {
                            goodguy.SendMessage("OnTouchEnded", hit, SendMessageOptions.DontRequireReceiver);
                        }
                    }
                    if (Input.GetTouch(i).phase == TouchPhase.Canceled)
                    {

                        foreach (GameObject goodguy in goodguys)
                        {
                            goodguy.SendMessage("OnTouchCancelled", hit, SendMessageOptions.DontRequireReceiver);
                        }

                    }
                }
            }
        }
    }

This is supposed to stop the raycast hitting anything below the big UI button I have, but its still being counted.