Detect touches on the top right corner of the screen

For my game, I have in the top right area of the screen there are 3 ui buttons, sound, Exit, pause buttons. These buttons are shown here:

82476-error1.jpg

I have created an empty UI game object and has given box collider2d component to it. and its area is shown in the green colour.
I’m using Single Tap, Swipe Up and Swipe Down for the player controls and its done using the code in the Update function:

for (var i = 0; i < Input.touchCount; ++i)
            {
                if (Input.GetTouch(i).phase == TouchPhase.Began)
                {
                    fp = Input.GetTouch(i).position;
                    lp = Input.GetTouch(i).position;
                }
                if (Input.GetTouch(i).phase == TouchPhase.Moved)
                {
                    lp = Input.GetTouch(i).position;
                    swipeDistanceX = Mathf.Abs((lp.x - fp.x));
                    swipeDistanceY = Mathf.Abs((lp.y - fp.y));
                }
                if (Input.GetTouch(i).phase == TouchPhase.Ended)
                {
                    angle = Mathf.Atan2((lp.x - fp.x), (lp.y - fp.y)) * 57.2957795f;
                    Debug.Log("Swipe Distance: " + swipeDistanceY);
                    Debug.Log("Tap count: " + Input.GetTouch(i).tapCount);
                    if (swipeDistanceY < 20)
                    {
                        if (Input.GetTouch(0).tapCount == 1)
                        {
                            PlayerScript.OnTap();
                            Debug.Log("Single tap..");
                        }
                    }
                    else if ((angle > 150 || angle < -150) && !GameScript.isCollectibleDragging)
                    {
                        Debug.Log("Down Swipe detected..");
                        PlayerScript.SwipeDown();
                        swipeDistanceY = 0;
                    }
                    else if((angle > -30 && angle < 30) && !GameScript.isCollectibleDragging)
                    {
                        Debug.Log("Up Swipe detected..");
                        PlayerScript.SwipeUp();
                        swipeDistanceY = 0;
                    }
                }
            }

Now the problem is when ever I tap on the buttons, the code for the single tap is being executed mostly. I need to block the player taps within the selected empty game object, so that when the player taps on the buttons, the buttons will work properly. Now the buttons are not detecting the presses, sometimes and the users are giving me negative feedback for that. How can I fix this issue? Any help would be appreciated very much.

You are definitely going to want to use Unity’s UI for this.

These work wonders. Take an hour or two and watch all the UI videos and use these instead. They will save you a bunch of time and open up new options for you and your game (in some cases, literally opening new options :wink: )

As a side note, you are using touch, so I’m assuming your game will be on mobile. Yes, buttons do work on mobile and they work very well (I used them for a little medical app I made for a doctor).

Cheers!