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:
Somehow access Unity’s own information on whether or not there was a click outside the screen area and Input is currently suspended
Some other way of registering mouse clicks so I can still check if the user clicked even when Input is disabled
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.
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.