- Home /
Disabling Mouse Look with Escape wont work?
var CursorLocked = true;
function Update()
{
if (Input.GetKey(KeyCode.Escape))
CursorLocked = false;
else
CursorLocked = true;
Screen.lockCursor = CursorLocked;
if(CursorLocked == false) {
GetComponent("MouseLook").enabled = false;
//disable begge mouse look scripts af spilleren
}
if(CursorLocked == true) {
GetComponent("MouseLook").enabled = true;
//disable begge mouse look scripts af spilleren
}
}
This is what I have got. It works, kind of.. I'm trying to make it so if mouselook is false so is mouselook. Thing is, it only sets CursorLocked to false if you hold down Escape?
Side note: I have 2 mouselooks, one on camera one on player prefab, how would I get to the child camera of the player to disable it's mouselook? I cant get this to work since I dont know what is going on and what to search on google. Any help appreciated, thanks.
Answer by Phles · Jan 17, 2014 at 02:46 PM
Hey there. The way that code is structured CursorLocked will only be true while you are holding down escape, you can change this by using Input.GetKeyDown(), but there are other problems with that code as well, in your else you set CursorLocked to true then use the the value in an if which will always be true as you just set it. Try the code below it should make escape toggle the CursorLocked value on each down press and enable/disable your mouselook component.
var CursorLocked = true;
function Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
CursorLocked = !CursorLocked
}
if (CursorLocked == true)
{
Screen.lockCursor = true;
GetComponent("MouseLook").enabled = false;
}
else {
Screen.lockCursor = false;
GetComponent("MouseLook").enabled = true;
}
}
Hope this helps.
Phill
Hmm, what this does, is when I start the game, you have to press ESCAPE to hide the mouse. That's one problem, the other is that to hide the mouse, you have to press escape again, but what I'm looking for is if you just click inside the game it hides it.
The old one worked that way, but it wouldn't activate it the way you made it do. Any ideas?
Ok I think I understand what you want to happen now, I edited the code in my answer accordingly.
Though if you only want to hide the cursor you may want to use Screen.showCursor = false;
ins$$anonymous$$d of the lock.
The lock makes it stay in the middle which is pretty important
Now it starts locked, but it also starts with mouselook disabled :P. It's al ot harder thna I figured it would be to just have something like this :O