Search code examples
unity-game-engineunity3d-gui

How to block game input from ui interaction?


I'm using Unity UI introduced in 4.6, and the button works fine. However, I have the game underneath controlled by touch. If the user touches the left of the screen, hero turns left, if the right side, it turns right. But if the user touches a button, it still turns right.

    if (Input.touches[0].position.x < Screen.width / 2) {
        turnLeft();
    } else {
        turnRight();
    }

how can I make this code unresponsive if the touch/mouse clicked the ui button?


Solution

  •         if (!EventSystem.current.IsPointerOverGameObject()) {
                 //same code here
            }