- Home /
How do I lock and then unlock the cursor using the same key?
All I want to do is lock the cursor to the middle of the screen at game start. That part is easy and it works. But no matter what I do, I cannot get a script to properly lock and unlock the cursor again, from the center of the screen, using just one key like 'R' for example. I have looked everywhere and read about every post I can find and this seems to be such a simple thing to do but it doesn't work.. Does anyone know how to script this properly? Here is my code and I have no idea why it wouldn't work but I am obviously misunderstanding something about the update function?
Also where should I be attaching this script - to the Player, the Player's main camera? Something else? This is an FPS styled game similar to Elder Scrolls and most survival horror games like Amnesia etc. I'd also like to get the tab key to bring up a menu eventually with the cursor unlocked of course but first things first... Thanks for any help, it's driving me crazy that I can't get this seemingly simple thing to work.
#pragma strict
private var wasLocked : boolean;
// lock the cursor at game start and set wasLocked to true. I'm thinking
// that we need to have two different states to check for when locking and
// unlocking within the Update method - we're using the same key to lock and unlock
// so I'm trying to use a boolean variable to add to the check.
function Start(){
Screen.lockCursor = true;
Screen.showCursor = false;
wasLocked = true;
}
function Update () {
// Unlock the cursor
if (Input.GetKeyDown (KeyCode.R) && wasLocked == true){
Screen.lockCursor = false;
Screen.showCursor = true;
wasLocked = false;
}
// Lock the cursor back to center of screen?
if (Input.GetKeyDown (KeyCode.R) && wasLocked == false){
Screen.lockCursor = true;
Screen.showCursor = false;
wasLocked = true;
}
}
Answer by supernat · Aug 30, 2014 at 03:21 AM
It's probably a misunderstanding of the compiler of what you really mean. It probably thinks you mean this:
(( Input.GetKeyDown (KeyCode.R) && wasLocked) == false)
It's good practice to surround your logic statements with explicit precedence, like this:
(Input.GetKeyDown (KeyCode.R) && (wasLocked == false))
Now, doing a test of something == false is not necessary. Use this:
(Input.GetKeyDown (KeyCode.R) && (!wasLocked))
Note that I surrounded the !wasLocked with (), but had I not done that, the compiler is smart enough to know you want to test !wasLocked first, so placing () around !variable is a matter of debate. I do it just to be explicit, but it's highly unlikely the compiler will be modified in a way that would break this. But with your original code, the compiler tested key down and wasLocked and looked for that to be false. You also need to correct the true line as well.
The simpler case is to just check if (Input.GetKeyDown(KeyCode.R)) first, then in a sub code block test for wasLocked or not.
Edit: you can place this on any GameObject, as long as that game object will be alive for the entire time that you want this behavior to be active. Just make sure it's not on 2 or more game objects, then it will just toggle on and off, for even numbers of game objects anyway.
Answer by Addyarb · Aug 30, 2014 at 03:22 AM
Try this:
if(Input.GetKeyDown(KeyCode.R)){
screenLock = !screenLock;
}
if(screenLock){
Screen.lockCursor = true;
Screen.showCursor = false;
}
else{
Screen.lockCursor = false;
Screen.showCursor = true;
}