If anyone could help with this it would be much appreciated. I’ve searched the net for hours looking at every article and post on unity mouse cursors I could find, I’ve found nothing to help solve this.
The Problem:
If I run my game in the editor the mouse pointer is locked and hidden exactly as it should. I unlock it in the pause menu, and it locks and hides again when I continue. All fine, and exactly as it should be.
But, when I make a build and run it (standalone, not in the editor) the mouse pointer is locked in the centre of the screen, but it’s still visible.
I created the simplest scene possible to test this. All I have in it is the following script attached to an empty game object:
using UnityEngine;
public class MouseTest : MonoBehaviour {
void Start () {
Cursor.lockState = CursorLockMode.Locked;
}
void Update () {
if(Input.GetKeyDown(KeyCode.L))
Cursor.lockState = CursorLockMode.Locked;
if (Input.GetKeyDown(KeyCode.U))
Cursor.lockState = CursorLockMode.None;
}
}
This works in the editor: Starts with the cursor locked and not visible. hit “U” key, is shows and I can move it around. Hit “L” key, it’s hidden and locked again. All fine.
Make a build. Starts with the cursor locked in the centre of the screen. But it’s still visible, just can’t move it.
I have a workaround for anyone else that gets the same issue.
Even though the unity documentation for Cursor.Lockstate reads:
“When Locked, the cursor is placed in the center of the view and cannot be moved. The cursor is invisible in this state, regardless of the value of Cursor.visible.”
This is true in the editor, but not in builds (for me at least).
If I also change the Cursor.Visible it works around this. So if the code above is changed like this, it works both in the editor and in build.
if (Input.GetKeyDown(KeyCode.L)) {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
if (Input.GetKeyDown(KeyCode.U)) {
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
This is exactly what I was already doing. But since upgrading to 2018.1 the cursor is no longer locked when I set it into the locked state. It’s invisible but can escape onto the editor panels, making the game unplayable.
@BabylonCandle : Had the same problem in Unity 2018.2. Thanks a lot for solving this.
OT, but for me, Unity does not respect any resolution settings in PlayerSettings when ‘Display Resolution Dialog’ is disabled or hidden. It always starts fullscreene window. Any idea about this?
I just wanna say thanks so so much guys for the help i swear i have been locking for solutions regarding this for almost a weeks and couldnt find a way to solve the invisible cursor but now i managed and all thanks to you guys. Finally i can finish up my game and submit my final project