- Home /
Remove unitys standard 'ESC' Function Cursor - Windows
How do I disable the default function of Unity that Escape brings up the mouse.
As when I press escape it pauses the game, Freezes the character and movement and brings up a paused UI if I press Escape againto exit the pause menu the mouse disapears for a breif second and then pops back up, I do gain access to the player again, but for some reason the cursor will continue to display the mouse regarless if I set Cursor.visable = false;
Current code that handles pausing the game.
if (isPaused && Input.GetKeyDown(KeyCode.Escape))
{
fpc.enabled = true;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
pausedCanvas.SetActive(false);
isPaused = false;
}
else if (!isPaused && Input.GetKeyDown(KeyCode.Escape))
{
fpc.enabled = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
pausedCanvas.SetActive(true);
isPaused = true;
}
You forgot to mention your target platform. If you talk about WebGL, there's no way to prevent that since pressing ESC is a security measure of the browser that you can not control or prevent. Any other platform shouldn't have this issue at all. If you're developing for WebGL you should find a different key to close a menu.
Sorry forgot to mention that, Windows and Linux
Are you actually testing in a build game? Or do you only test inside the Unity editor? The Unity editor has several hotkeys it reacts to even when in playmode / gameview. The Unity editor is there to design and edit your game. So all hotkeys in the editor are there to help the developer. I haven't made any FPS tests in the recent Unity versions so I can't tell if this is actually an issue in a build. The last time I made a fps demo was years ago and I can't remember any issues with the escape key.
Answer by Bunny83 · Apr 03, 2020 at 11:40 AM
Ok since you put up an example (without any code) I decided to quickly boot up my Unity test project and throw together a minimalistic FPS setup. A single script on a character controller capsule. The main camera is simply a child of the player. This is the result after a windows standalone build
As you can see by pressing the ESC button when in the menu I just set Cursor.visible to false and Cursor.lockMode to "Locked". Everything seems to work as it should. Note that when testing inside the editor pressing the ESC button does not lock / hide the cursor again as mentioned in the comment above. However the build works as it should.
I used Unity version 2019.2.14f1. I'm on a Windows 10 64Bit machine (Intel i7 with a relatively old NVIDIA GeForce GTX 750 Ti).
I currently don't have a running linux machine (beside my Raspberry PI server with rasperian) so I can't test a linux build. I can not reproduce your issue. If you want any further help with your issue you have to provide much more information and much more details on your specific case.
If you had of asked for me to show the code, I would of happily supplied it then and there. but its as basic as you can get so I didnt think it would be neccesary.
if (isPaused && Input.GetKeyDown(KeyCode.Escape))
{
fpc.enabled = true;
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
Cursor.visible = false;
pausedCanvas.SetActive(false);
isPaused = false;
}
else if (!isPaused && Input.GetKeyDown(KeyCode.Escape))
{
fpc.enabled = false;
Cursor.lockState = CursorLock$$anonymous$$ode.None;
Cursor.visible = true;
pausedCanvas.SetActive(true);
isPaused = true;
}
System: Win10 x64, Unity 2019.2.16f1, 16gb RA$$anonymous$$, i5 4460 3.2ghz, NVIDIA GeForce GTX 1060 6gb
Game: Using Unitys First Person Character with the pause script attached.
Well, is this snippet in Update and not in FixedUpdate or OnGUI or some other callback that doesn't run in sync with actual input frames? Also are you sure this is the only place in your entire project where you change the lockState and the cursor visibility? What's your used Unity version? You seem to use the old input system (like I do as well). However have you enabled the new (preview) input system?
Note that I don't need all this info since I'm not the one with the problem. You want your issue to be fixed and we invest our spare time trying to help you. Since you have that issue and I don't there has to be something different on your side. However we can only work with what you give us.
Yes this is the only thing in the entire project that enables and disables the mouse on pressing escape and Yes it is in Update() I tried FixedUpdate, but that sometimes wouldnt even open the pause menu, I did have other functions that closed opened UI's but had removed them to test if they were the issue, but still get the same result. I dont know what further information I can provide unless you let me know what it is you need.
Your answer
