How to exclude specific touch area!

So I have a code where if the users touches on left-side of screen he turns left and when in right-size he turns right. That is coded in script.

I have placed a mute sound button on right corner. But when ever I press on it, the players turns right as it is sensed that they user touched right side.

I tried using buttons but it became glitchy delaying the actions on onClick.

Is there a workaround for it?

Regards.

 if (rotationType == RotationType.FixedAngle)
                    {
                        
                        //If we have touched the right side of the screen.
                        if (touch.phase == TouchPhase.Began && touch.position.x > Screen.width / 2)
                        {
                            wantedRotation.z += fixedAngle;
                        }

                        //If we have touched the left side of the screen.
                         if (touch.phase == TouchPhase.Began && touch.position.x < Screen.width / 2)
                         {
                        wantedRotation.z -= fixedAngle;
                         }
                    }

Try addin this to your code, it checks if the finger is pointing over a UI element

    public bool IsPointerOverUI(int fingerId)
    {
        EventSystem eventSystem = EventSystem.current;
        return (eventSystem.IsPointerOverGameObject(fingerId)
            && eventSystem.currentSelectedGameObject != null);
    }

@Spip5 This is my code could you help me to properly set the code you mentioned. I am a kinda beginner in this.

Regards.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using DG.Tweening;

public enum RotationType
{
    FixedAngle,
    FreeRotate
}

/// <summary>
/// Player related Input, movement and rotation behaviour.
/// </summary>
public class PlayerController : MonoBehaviour
{
    public GameObject touch1;

    //The script responsible for keeping track of the score.
    private ScoreManager scoreManager;

    //The script responsible for all common game and UI events.
    private EventManager eventManager;

    //The rigidbody attached to the player.
    private Rigidbody player;

    

    [Header("Player Movement")]

    //The behaviour of the player when rotating around the tunnel.
    [Tooltip("The way in which the player rotates around the tunnel interior.")]
    public RotationType rotationType = RotationType.FixedAngle;

    //The initial speed of the player.
    [Tooltip("The speed of the player object.")]
    public float speed = 12f;

    //The speed at which the object will rotate.
    [Tooltip("The speed at which the player object will rotate.")]
    public float rotationSpeed = 10f;

    //The value in degrees that the player will be rotated by if the rotationType is Fixed Angle.
    [Tooltip("The Fixed Angle rotation amount in degrees.")]
    public float fixedAngle = 45f;

    [Header("Miscellaneous")]

    //The Layer that the threat objects belong to.
    public LayerMask threatLayer;

    //Should input be accepted?
    public bool acceptInput = false;

    //The initial starting rotation of the player.
    Vector3 wantedRotation;

    //The start and end points for the LineCast that will detect the distance to a threat in front of the player.
    private Vector3 detectorPosition;
    private Vector3 endPosition;

    private void Awake() {

        //Cache the ScoreManager script.
        scoreManager = GameObject.FindObjectOfType<ScoreManager>();

        touch1 = GameObject.Find("Mute");

        //Cache the EventManager script and populate the necessary variables.
        eventManager = GameObject.FindObjectOfType<EventManager>();
        eventManager.playerController = this;
        eventManager.scoreManager = scoreManager;

        //Get the RigidBody attached to this object.
        player = GetComponent<Rigidbody>();
    }

    void Update()
    {
       
        if (acceptInput)
        {
            if (!Application.isMobilePlatform)
            {
                #region KEYBOARD INPUT.

                #region FIXED ANGLE INPUT.

                //If we are using the fixed angle rotationType.
                if (rotationType == RotationType.FixedAngle)
                {
                    //If we press the right arrow key, set the new wantedRotation.
                    if (Input.GetKeyUp("right"))
                    {
                        wantedRotation.z += fixedAngle;

                        //Tell the AudioManager that we have moved so that it can play the required sound effect.
                        AudioManager.moved = true;
                    }

                    //If we press the left arrow key, set the new wantedRotation.
                    if (Input.GetKeyUp("left"))
                    {
                        wantedRotation.z -= fixedAngle;

                        //Tell the AudioManager that we have moved so that it can play the necessary sound effect.
                        AudioManager.moved = true;
                    }
                }

                #endregion
                #endregion  

            }

            if (Application.isMobilePlatform)
            {
                #region TOUCH INPUT.

                foreach (Touch touch in Input.touches)
                {
                    #region FIXED ANGLE INPUT

                   

                    if (rotationType == RotationType.FixedAngle)
                    {
                        
                        //If we have touched the right side of the screen.
                        if (touch.phase == TouchPhase.Began && touch.position.x > Screen.width / 2)
                        {
                            wantedRotation.z += fixedAngle;
                        }

                        //If we have touched the left side of the screen.
                         if (touch.phase == TouchPhase.Began && touch.position.x < Screen.width / 2)
                         {
                        wantedRotation.z -= fixedAngle;
                         }
                    }

                    #endregion

                }

                #endregion

            }
        }
    }

    void FixedUpdate()
    {
        //Move forward at a constant speed.
        player.MovePosition(transform.position + transform.forward * Time.deltaTime * speed);

        //Rotate the player object by the specified angle.
        player.MoveRotation(Quaternion.Lerp(transform.rotation, Quaternion.Euler(wantedRotation), Time.deltaTime * rotationSpeed));

    }

    //Used to detect a threat or a near miss.
    void OnTriggerEnter(Collider threat)
    {
        //Does the object that we have collided with belong to the threat layer that we have set?
        if (((1 << threat.gameObject.layer) & threatLayer) != 0)
        {
            //Disable the threat we have collided with.
            threat.gameObject.SetActive(false);

            //If we aren't accepting input, we do not want to provide feedback.
            if (acceptInput)
            {
                //If we have collided with a threat, provide visual feedback.
                Camera.main.DOShakePosition(.3f, .2f, 50, 50);

                //Play the imact sound asociated with a game over event.
                AudioManager.failed = true;
                

                //Game Over.
                eventManager.GameOver();
            }

        }

    }
}

This doesn’t seem to work, do you have a solution? Here is my code that doesn’t work:

private void Update() {

        if (deviceChecker.isMobile) {
            for (int i=0;i<Input.touchCount;++i) {
            
                if (Input.GetTouch(i).phase == TouchPhase.Began) {
                    Touch touch = Input.GetTouch(0);
                    cursor.transform.position = touch.position;
                    Vector2 direction = Camera.main.ScreenToWorldPoint(cursor.transform.position) - transform.position;
                    float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
                    Quaternion rotation = Quaternion.AngleAxis(angle - 90f, Vector3.forward);
                    transform.rotation = rotation;
                }
                if(Time.time >= shotTime) {
                    Instantiate(projectile, shotPoint.position, transform.rotation);
                    cameraAnim.SetTrigger("shake");
                    shotTime = Time.time + timeBetweenShots;
                    
                    foreach(Touch touch in Input.touches) {
                      if(IsPointerOverUI(touch.fingerId)) {
                            return;
                        
                        }
                    }
                }
            }

         public bool IsPointerOverUI(int fingerId) {
         EventSystem eventSystem = EventSystem.current;
         return (eventSystem.IsPointerOverGameObject(fingerId)
          && eventSystem.currentSelectedGameObject != null);
    }

I found this worked best for me.

For some reason the final phases of the touch action ( TouchPhase.Ended & TouchPhase.Cancelled) were still dodging the check. Perhaps they are not registering as occurring over the UI. So I had to add the second if statement to specifically to deal with these phases.

using UnityEngine.EventSystems;

void Update ()
{
	//Exit if touch is over UI element.
	foreach (Touch touch in Input.touches)
	{
		int id = touch.fingerId;
		if (EventSystem.current.IsPointerOverGameObject(id)) { return; }
		if ((touch.phase == TouchPhase.Ended) || (touch.phase == TouchPhase.Canceled)) { return; }
	}
}