Avoid touch input when pressing the "Pause" button

The player moves whenever I touch the screen in my game. The issue is that when I press the “Pause” button, the player moves for as long as I hold down the button before releasing.

How can I make the touch input only count for a certain range of the screen? I have read about raycast but I’ve got no clue how to implement it in my game.

void Update() {
      if (Input.GetMouseButton(0) && Time.timeScale != 0) {
         rb.velocity = new Vector2(moveSpeed, moveSpeed);
         transform.rotation = Quaternion.Euler(0, 0, -42);
      } else {
         rb.velocity = new Vector2(-moveSpeed, moveSpeed);
         transform.rotation = Quaternion.Euler(0, 0, 42);
      }

Thank you in advance!

There are 2 simple ways of doing it.

  1. Set Time.timeScale = 0; when you press the pause button. Doing that will freeze the game completely and no matter how much input you take the player won’t move. Set Time.timeScale = 1 to unpause the game.
  2. If you don’t want the game to be freeze and just want player to stop taking input. I am assuming that you are using an UI Button for pause button. if yes, then use EventSystem.current.IsPointerOverGameObject() to check if the mouse is over an UI element.

This code should work for you.

    if (Input.GetMouseButton(0) && Time.timeScale != 0 && !EventSystem.current.IsPointerOverGameObject())
        {
            Debug.Log("Move your player!");
        }