- Home /
Cursor.lockState not working
Simple code I've got attached to an empty game object in my scene, however the cursor does not become confined in either the editor or a build. Here is the code:
void Start () {
Cursor.lockState = CursorLockMode.Confined;
}
void Update () {
if (Input.GetKeyDown(KeyCode.Escape)) {
if (Cursor.lockState == CursorLockMode.Confined) {
Cursor.lockState = CursorLockMode.None;
} else {
Cursor.lockState = CursorLockMode.Confined;
}
}
}
Answer by bartm4n · Dec 31, 2015 at 06:55 AM
I had some similar problems with 5.3.1p1 and I made the following observations:
1) Transitioning from Locked to Confined does not work without first transitioning to None. The lockState will report correctly, but the behavior will be for the cursor to just jiggle in the center of the screen.
Cursor.lockState = CursorLockMode.Locked;
...
if (Cursor.lockState != CursorLockMode.Confined) {
//Going directly from Locked to Confined does not work
Cursor.lockState = CursorLockMode.None;
Cursor.lockState = CursorLockMode.Confined;
}
...
2) Transitioning to Locked automatically hides the cursor
I know that this isn't 100% the same as your problem, but I think the issue is at least semi-related and your question came up when I Googled the problem.
I was fighting with weird cursor behaviour for an hour and found your comment. Beautiful, thanks.
As of the current version of Unity (2019.2.18f1) this still seems to be the case. If I go from Locked to Confined in the editor Game window, the cursor will become visible but will be stuck in the center (though it'll jitter a bit). Doing Locked > None > Confined will free my cursor in the editor Game window (though it doesn't get Confined in editor window, but that a known and expected thing). Thanks.
Answer by allenallenallen · Aug 04, 2015 at 03:41 AM
Try changing the Escape key to another button. Escape usually unlocks the cursor by default in the web player. And cursor doesn't really work the way it should in the editor so if you want to test out cursors, you should usually try it on a build.
I tried changing the Escape key, which didn't help. Builds don't seem to help either.
All right, this is probably a Unity bug then. Which version of Unity are you using? It might be fixed in the newest version.
People reported the same bug: http://forum.unity3d.com/threads/cursor-lockstate-troubles.278221/
Answer by frozendog_bren · Aug 04, 2015 at 04:14 AM
I'm using this... Give it a try. Make sure to use "using System;" as library!
using UnityEngine;
using System.Collections;
using System;
public class GameMaster : MonoBehaviour {
public bool lockCursor = true;
void Awake () {
Cursor.visible = (false);
}
void Update () {
if(Input.GetKeyDown(KeyCode.M))
{
lockCursor = !lockCursor;
}
if (lockCursor) {
Cursor.lockState = CursorLockMode.Locked;
} else {
Cursor.lockState = CursorLockMode.None;
}
}
}
It still doesn't work. When I print the state of the cursor, Unity tells me that the cursor IS locked, the cursor just isn't behaving the way that it's state says it should be.
I'd like to mention that using System is just a shortcut for writing code. It is processed during compile time, so including this line will not affect runtime behavior at all. ($$anonymous$$issing this line will cause compiler errors too, so Unity won't let you run the program anyways.)
The using keyword saves you from having to type out the namespace every time. For example, you would have to type System.Enum ins$$anonymous$$d of Enum if you don't have using System.
Answer by elJoel · Mar 30, 2017 at 04:33 PM
This is old but here is how I Lock the Cursor when pressing Escape:
if (Input.GetKeyDown("escape"))
{
StartCoroutine(DisableCursor());
}
private IEnumerator DisableCursor()
{
yield return new WaitForEndOfFrame();
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
Answer by arutyunef · Jul 01, 2019 at 09:07 PM
None of the answers helped me, but later I found out this code works like a charm. Whenever u want to fix it, do a coroutine with FixMouse. Don't know why this code works specifically for 2 * Time.deltaTime
IEnumerator FixMouse ()
{
Cursor.lockState = CursorLockMode.Locked;
yield return new WaitForSeconds ( Time.deltaTime * 2 );
Cursor.lockState = CursorLockMode.Confined;
}
void Start ()
{
StartCoroutine ( FixMouse () );
}
Your answer
Follow this Question
Related Questions
Restrict the cursor to a window in Unity 5 0 Answers
Cursor Lockmode only works once? 1 Answer
Cursor bug 0 Answers
Keep Cursor at center and project interactions to cursor position on mobile touch devices (IOS) 0 Answers
Cursor.lockState does not work 2 Answers