I have a problem with Locking the Cursor (Unity)?

Viewed 5287

The following code locks the cursor only when I left click my mouse after pressing Escape Key. Can anyone suggest me a way to overcome this problem?

public class lockCursor : MonoBehaviour
{
    public bool locked = true;
    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked; 
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {

            if (locked == false)
            {
                Debug.Log("locked");
                Cursor.lockState = CursorLockMode.Locked;
                locked = true;
            }
            else
            {
                Debug.Log("Unlocked");
                Cursor.lockState = CursorLockMode.None;
                locked = false;
            }
        }
    }
}
2 Answers

As said in the API for Cursor.lockState

In the Editor the cursor is automatically reset when escape is pressed, or on switching applications.

Due to escape having a special role itself within the Unity Editor the GameView loses focus when pressing it.

There is not really a (trivial) way around this since it would require to intercept they keypress before the Unity Editor itself gets the chance to handle that escape press. (Which would always happen before it is passed on to the player -> your code.)


What you can do however would be simply use a different KeyCode only for the editor and escape only in a build like e.g.

#if UNITY_EDITOR
    if(Input.GetKeyDown(KeyCode.E))
#else
    if(Input.GetKeyDown(KeyCode.Escape))
#endif
    {
        ...

You can replace the E with any other KeyCode you wish to use to "simulate the escape key" only within the Unity Editor itself. (Also see Platform dependent compilation)

As derHugo mentioned in his comment, your issue lies in the API for Cursor.lockState where it says:

"In the Editor the cursor is automatically reset when escape is pressed, or on switching applications. In the Standalone Player you have full control over the mouse cursor, but switching applications still resets the cursor."

When you go to press Escape, it removes you from the application/Game Window. This is why it'll appear like your cursor is no longer locked. This is also why it goes into it's locked state as soon as you click back into the game. The same thing can be demonstrated by using your code, starting the game and then switching into another window on another monitor. Then move your mouse back over to the screen where the Game Window is. It'll appear as though it is no longer in a locked state... that is until you click in the game and it recognizes the cursor again.

To solve your problem, I would recommend using any key other than KeyCode.Escape. Maybe KeyCode.Space like the API shows.

Also, to further demonstrate the issue here, once you switch the code to use KeyCode.Space, press Space to lock the cursor and then try pressing Escape. Again you'll see that it appears to be unlocked until you press the Left Mouse Button in the Game Window again. This is because the Game thinks that you are no longer in the Game Window once you press Escape.

Hope this helps!

Related