Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Is there a way to check if the user clicked outside the game window

Discussion in 'Scripting' started by Hellothere_1, Jun 26, 2020.

  1. Hellothere_1

    Hellothere_1

    Joined:
    Sep 18, 2018
    Posts:
    32
    I'm making an RTS where one way of controlling the camera is to move the mouse towards the edge of the screen to move the camera in that direction.

    The problem is that this gets really annoying when you're trying to adjust something in the editor because doing so will move the mouse outside the screen area and cause the camera to fly off into the distance as you're making the adjustments.

    My idea for a solution was to check if the user clicked outside the bounds of the screen area, and then suspend camera movement until he clicked inside the area again.

    The problem is that Unity itself seems to have already implemented something similar to prevent accidental inputs, which freezes the entire Input class if you clicked outside the screen area, so my own check never registers the click.

    Basically what I need is one out of two things:
    1. Somehow access Unity's own information on whether or not there was a click outside the screen area and Input is currently suspended
    2. Some other way of registering mouse clicks so I can still check if the user clicked even when Input is disabled
    Any ideas?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Why clicks? If you have the ability to determine whether the mouse is outside the game window during a click, you should also be able to make that same determination passively without a click, and react as soon as the mouse moves outside the window.

    I suspect you'll find that Unity clamps the mouse position to always look like it's within the window, though. (I haven't done any careful tests, but that was the impression I got from various historical incidents.)

    You could create a one-pixel "dead zone" at the edge of the window, so that the game scrolls if the mouse is near the edge but not if it's exactly at the edge. This is probably not what you want when playing the game fullscreen, though.

    If you want to detect when the user clicks into another application--that is, when the game loses focus--you can use OnApplicationFocus. I don't think that will trigger in play mode when you click in other parts of the editor, but I'm not completely certain.
     
    Hellothere_1 likes this.
  3. Hellothere_1

    Hellothere_1

    Joined:
    Sep 18, 2018
    Posts:
    32
    This is exactly what I was looking for. It works perfectly, even when clicking on other parts of the editor. Thanks!


    It actually doesn't. Unity is perfectly happy to return mouse positions outside the screen area. The first thing I tried to resolve the problem was to have the camera stop moving once the mouse gets further than 100 pixels away from the game window.
    It did work, kind of. However, quite often you'd accidentally move the mouse a bit too far and the camera stops moving which is really annoying.
    At the same time, a lot of the stuff you usually click on while the game is active (inspector and hirarchy) are partially inside the 100 pixel margin, so if you click on something there, Input.MousePosition gets frozen at that position and the camera keeps moving until you click inside the game window again to unfreeze it.
    That's why I thought detecting clicks outside the game area would be better.