How to prevent the player movement when pressing the ui

I assume you solved it previously? but I am not using your project but my own…

thanks

There is a check: EventSystem.current.IsPointerOverGameObject(). It will return true if the pointer is over UI. You can check it when you click somewhere

This is correct. Before acting on any movement clicks, check the IsPointerOverGameObject(). Do not act if this is true.

It might be better, however, to suspend operations when a window like the DialogueUI or InventoryUI is open…
The simplest way to accomplish this is to change the Time.timeScale to 0 and then return it to 1 when the Dialogue is closed. The only problem with this is that either of these windows (or the shop or trait window in the next course) could be still be open when you close one of the windows.

Here’s what I’ve done to make this work:

public class WindowFreezer : MonoBehaviour
{
    void OnEnable()
    {
         Time.timescale = 0;
     }
     
     void OnDisable()
     {
          foreach(WindowFreezer freezer in FindObjectsOfType<WindowFreezer>())
          {
               if(freezer == this) continue;
               if(freezer.enabled) return;
          }
          Time.timeScale = 1.0f;
      }
}

I put this on each of the UI Objects like HidingPanel (contains InventoryUI and EquipmentUI), DialogueUI, etc. When one is opened, the game is automatically paused. When it closes, if any windows are still open, the timescale is not reset, only if all of the windows are closed does the game quit being paused.

thanks!!!

I have a events invoked movement and interaction (with the new input system) so I tweaked your code a bit:

public class WindowFreezer : MonoBehaviour
    {
        public event Action onUIWindowOpen;
        public event Action onAllUIWindowsClosed;
        void OnEnable()
        {
            onUIWindowOpen();
        }

        void OnDisable()
        {
            foreach (WindowFreezer freezer in FindObjectsOfType<WindowFreezer>())
            {
                if (freezer == this) continue;
                if (freezer.enabled) return;
            }
            onAllUIWindowsClosed();
        }
    }

Privacy & Terms