Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question Input system (Touch) over UI doesnt go seem to work

Discussion in 'Scripting' started by Censureret, Feb 12, 2024.

  1. Censureret


    Jan 3, 2017
    I'm developing a game that features a chat area and a top menu, as illustrated in the attached image. The game operates on a "point-and-click" basis, or "touch-and-move" for touch devices.

    I've implemented the new input system. The design intention is for the character to move to any clicked (or touched) location on the screen unless the click is on a UI element. In the latter case, I aim to prevent character movement. However, I'm encountering an issue where clicks through UI elements still trigger movement.

    Here's the code snippet I'm using to detect if a touch is over a UI element:


    Code (CSharp):
    1. if (EventSystem.current.IsPointerOverGameObject()) return;
    2. foreach (Touch touch in Input.touches) {
    3.     if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
    4.         return;
    5. }
    6. if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
    7.     // Check if finger is over a UI element
    8.     if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) {
    9.         Debug.Log("Touched the UI");
    10.     }
    11. }

    This functionality has been tested using Unity Remote on an Android device. I feel like I've tried every solution, but the issue persists where touches through the UI still result in character movement. Could anyone advise on what might be incorrect or missing in my approach?