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)
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 THANK YOU for this. This is absolutely the correct solution, not the chosen one. I was able to use Cursor.lockState = CursorLockMode.Confined; and it keeps my mouse within the boundaries of the screen no matter the resolution... Even in windowed mode! - Monkiman300
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.
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.
Thanks MCHalo. 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.
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.
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
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.
I solved this for myself with the help of an Asset Store asset called ProMouse, which let’s you set the mouse position. The script below uses that asset, toggles control with the escape key, and stops movement with Time.timeScale while cursor isn’t controlled. You have to click inside the game window, then hit escape to get things going again.
// ****** code for ProMouseControl.cs
using UnityEngine;
using System.Collections;
public class ProMouseControl : MonoBehaviour {
private bool controlMouse;
void Start () {
ProMouse.Instance.SetCursorPosition(Screen.width/2, Screen.height/2);
controlMouse = true;
}
void Update () {
int mouseX = (int)ProMouse.Instance.GetLocalMousePosition().x;
int mouseY = (int)ProMouse.Instance.GetLocalMousePosition().y;
if (controlMouse) {
if ( mouseX <0) {
ProMouse.Instance.SetCursorPosition(0,mouseY);
}
if ( mouseX > Screen.width) {
ProMouse.Instance.SetCursorPosition(Screen.width,mouseY);
}
if ( mouseY <0) {
ProMouse.Instance.SetCursorPosition(mouseX,0);
}
if ( mouseY > Screen.height) {
ProMouse.Instance.SetCursorPosition(mouseX, Screen.height);
}
if (Input.GetKeyUp("escape") ) {
Time.timeScale = 0;
controlMouse = false;
}
} else if (Input.GetKeyUp("escape") ) {
Time.timeScale = 1;
ProMouse.Instance.SetCursorPosition(Screen.width/2, Screen.height/2);
controlMouse = true;
}
}
}
Hi,all my projects using cursorLocked are stucked. The mouse stay there shaking at the right of the gamescreen. There has been two upgrades before this problem happens. i upgraded to unity 2019.2 and My windows 10 upgraded to ver 1903
Nothing goes well anymore, and i don’t know how to remediate.
Here is the simple code that was still working last week…
This is quite a bit old, but nonetheless, My issue was the mouse wouldn’t lock after minimizing the engine, and coming back to it, while it was in play mode.
I found the answer to why it was happening to me, Here
Basically what was going on was, I didn’t have the option: “Run in background” enabled in the player settings.
This may also help others that are running into this as well; or at least can check it off the list of possible fixes.
Wow. Lots going on here. If you have PlayMaker installed and PlayMakerGUI in your hierarchy it defaults to controlling the mouse cursor. Untick the box in the inspector that says ‘Control Mouse Cursor’.
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)
– Dreamer:) thanks for the ideas :)
– MC_HALO