0
\$\begingroup\$

Not too sure how to ask this, I'll try my best.

Currently, I'm working on a First-Person Controller. I'm adding functionality for a pause menu so that when you press escape, it will open the pause menu, unlock the cursor, and make it visible. If you press escape again, while the pause menu is open, it will do the opposite: hide the cursor and lock it again.

The problem I'm running into is that Unity already kind of does this. It doesn't matter if you're in the editor or if your game is built: whenever you press escape, it will unlock the cursor and show it, so you can exit the game (in a built application) or get out of play mode (in the editor).

I'm 99% sure this is Unity's default functionality. I have looked in Unity itself and haven't found a way to turn it off, and I have also looked online. Any help would be greatly appreciated.

\$\endgroup\$
2
\$\begingroup\$

If you are using Unity's FPSController then this post might have the solution https://forum.unity.com/threads/how-to-disable-escape-key-to-unlock-the-cursor-fix.511674/

If the forum post does not solve your problem, the error probably lies in your own escape code triggering twice (or fails to set the correct value). Edit: in that case please provide your code.

According to the documentation https://docs.unity3d.com/ScriptReference/Cursor-lockState.html the unlock on escape should only happen in editor.

\$\endgroup\$
0
\$\begingroup\$

This seems to be a problem related to the code in Unity's standard assets: In order to lock the cursor, you would set the CursorLockMode to Locked;

Your FPSController script (or whatever script is attached to your player) is checking wheather the "escape" key is pressed and setting the cursor mode to None by doing:

if(Input.GetKey(KeyCode.Escape))
    Cursor.lockState = CursorLockMode.None; // free the cursor

Contrarily, when pressing the left mouse button (also known as "mouse button 0"), it's setting the cursor state to locked:

if(Input.GetMouseButtonDown(0))
    Cursor.lockState = CursorLockMode.Locked; // lock the cursor

If you don't want this code to be managed within the fps controller (which i assume you don't, given the situation), you just need to remove the bit of code where the cursor lockState gets changed from the FPSController script;

This obviously also applies if you are not using the standard assets.
This is not Unity's standard behavior, so i suggest you look for any eventual script that might be modifying the cursor lockState.

\$\endgroup\$
  • \$\begingroup\$ Unfortunately, even with, say, a static camera (with no scripts, etc.) it still happens. Thanks for the help though. \$\endgroup\$ – ThriftyDonut Jan 30 at 23:24
  • 1
    \$\begingroup\$ @ThriftyDonut how are you locking the cursor without any scripts? \$\endgroup\$ – DMGregory Jan 31 at 1:20
  • \$\begingroup\$ @DMGregory I'm not, but Unity still unlocks and shows the cursor when I press escape and relocks and hides it when I click in the play area. \$\endgroup\$ – ThriftyDonut Jan 31 at 1:30
  • \$\begingroup\$ Then you have code somewhere. We can't find it for you because we're not clairvoyant. \$\endgroup\$ – Draco18s Jan 31 at 18:27
  • \$\begingroup\$ @Draco18s It happens even in an empty scene, with just a camera. It's Unity's default functionality. \$\endgroup\$ – ThriftyDonut Feb 8 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.