Problem with touch input unity 2017

Hello guys, I’ve got a problem with touch functions in unity.

i’ve got this code for the movement of the player inside the update function (when the touch begins, it works):

if((Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) && !paused){
   rb.velocity = Vector3.zero;
   rb.AddForce(UpForce, 0, 0);
   lerpingStarted = false;
}

Everytime i want to click the pause button in my game (it is a simple button inside a canvas, attached to it there is a script that change the variable “paused” to true if the button is clicked while not in pause), the game, before it pauses, detect the touch (or the mouse click) and makes the player move. I want to avoid this.

My goal is to not let the game detects the touch if i click the pause button. I tried to use raycast attaching a collider to the button, but the result was the same. What can i do?

Thank you in advance.

If you’re only using the graphics raycaster (and not physics or physics2d raycasters), and you have an event system and this button is in the UI (canvas) …Then, you can add this to your check/if statement:

if(!EventSystem.current.IsPointerOverGameObject())

:slight_smile:

If you use other raycasters, that won’t work reliably as it will count others as ‘over’. You’d have to override the method in a derived class, or create a new method.

Hopefully that helps :slight_smile:

For the record, this isn’t directly an issue with touch alone, nor Unity 2017 specifically.

1 Like

Thank you for your answer, it works like a charm! I don’t know why but nobody suggest this method to solve this problem.

I included the “touch” and “unity 2017” to the title because I didn’t know if it was a problem with them.

I’m glad it worked for you. :slight_smile:

1 Like