- Home /
how to stop mouse going out of the screen ?
Hi, guys i was wondering if you could please help me. Basically the problem that i am having is with my mouse cursor. what i want to do is if the player runs the game in the build view and his window is in not in full mode i want to stop them from leaving the screen. so what i mean is when ever you make a build and run it you can always get you mouse cursor out of the screen and start touching the X button and Minimize and etc. So what i want to do is if the game starts the mouse is hidden and if they try to go out of the screen i don't want that to happen. Unless they pause the game and the mouse cursor will go back the way it was.
here what i have come up with so far dont laugh lol i will be really happy if some one could really help me :-) thanks in advance MCHALO :)
My script:
function Awake() {
Screen.showCursor = false;
}
2 approaches I can think of: 1. Try to find if there is such settings in editor 2. If not, then a editor script is required to do it. (I don't noe how to write one)
Answer by Joshua · May 04, 2011 at 03:40 AM
All you can do is use Screen.lockCursor to lock the cursor in the center of the screen and hide it. Don't just do it in your Awake function because if a player presses escape you'll have to reinitialise it. Do it OnMouseDown possibly.
Does not work :) the mouse is still moveable and goes out of the screen :)
Of course it does work, how can you say it doesn't. It's a piece of code that does EXACTLY what you asked. You're just using it wrong. Like I said don't stick it in Start or Awake.
I solved the problem my self i did it using start and update but thanks for the help.
Thanks $$anonymous$$CHalo. I had the same problem, I put:
Screen.lockCursor = true;
Despite being full-screen and stating that code in the start function the mouse would still go onto my other screen in-game. As you said, putting that code in the Update(){} function solved the problem. Thankyou.
Answer by jcrosby · Apr 05, 2016 at 05:37 AM
I know this is an old answer, but it helps to keep old answers update with current information. I found a much easier and up to date solution:
Cursor.lockState = CursorLockMode.Confined; // keep confined in the game window
Cursor.lockState = CursorLockMode.Locked; // keep confined to center of screen
Cursor.lockState = CursorLockMode.None; // set to default default
I had to log in to say THAN$$anonymous$$ YOU for this. This is absolutely the correct solution, not the chosen one. I was able to use Cursor.lockState = CursorLock$$anonymous$$ode.Confined; and it keeps my mouse within the boundaries of the screen no matter the resolution... Even in windowed mode!
$$anonymous$$onkiman300
Thank you! Simple and perfect answer! Too bad the chosen answer is not yours :/
Quite late. Thanks though. Amazing and the most correct answer. Thank you so much.
Answer by amirabiri · Jun 19, 2012 at 01:15 AM
I found a better solution for this problem using PInvoke. This would work only on windows, but windows is the main platform that suffers from this problem I believe:
#if UNITY_STANDALONE_WIN
[DllImport("user32.dll")]
static extern bool ClipCursor(ref RECT lpRect);
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
#endif
public void Start()
{
RECT cursorLimits;
cursorLimits.Left = 0;
cursorLimits.Top = 0;
cursorLimits.Right = Screen.width - 1;
cursorLimits.Bottom = Screen.height - 1;
ClipCursor(ref cursorLimits);
}
Just to clarify, this solution would limit the mouse cursor to a certain region at the operating system level, giving a smooth result without compromising the OS' natural mouse movement.
Thank you! This solution works great to prevent mouse leaving display when using 2 monitors. However it works in fullscreen mode, in windowed RECT boundaries are calculated wrong, don't know why.
Answer by terdos · Jun 10, 2011 at 04:23 PM
As far as I know you cannot restrict the mouse from going outside the window of the application. However as stated above it is possible to lock the cursor to the center of the screen.
http://unity3d.com/support/documentation/ScriptReference/Screen-lockCursor.html
At this point you will have to draw your own cursor and handle your own cursor events. You can still calculate the position of the cursor using Input.GetAxis("Mouse X") and Input.GetAxis("Mouse Y") assuming they are configured in Project Settings->Input. You will likely have to detect mouse button clicks using Input.GetMouseButtonXXXX().
Yeah, this works fine and you can of course restrict the position of your own cursor to within the screen. The big drawback though, is that you can no longer use the GUI system. It's why I decided to abandon the goal of restricting the mousepostion in my own question about this http://answers.unity3d.com/questions/59123/lock-mouse-position-with-a-rect-possible.html
Answer by thenachotech1113 · Jun 19, 2012 at 01:24 AM
what about:
Screen.lockCursor = true;
i wold put this in function start and ad to the update a button to release it
The problem with lockCursor is that it completely cancels the natural operating system mouse movement. For and FPS that's fine. But if you want to use the mouse for something else (like let's say a strategy game) then if you use lockCursor you then have to build your own cursor, which is hard. You have to take care of acceleration and smoothing correctly and account for the user's preferences.
Your answer
Follow this Question
Related Questions
Cursor disappears in unity. 1 Answer
A node in a childnode? 1 Answer
C# Track Mouse Cursor Movement Speed 2 Answers
custom mouse changing when hovering over objects 1 Answer
Unity 3D Screen.lockCursor Problems 2 Answers