- Home /
 
Locking and Unlocking a cursor...
Locking and Unlocking a cursor using the Tab key (to unlock) and clicking on the screen (to relock).
I have a short script I wrote to lock a cursor at the start of the game. However, with many failed attempts, I have tried to implement a button (Tab) that will unlock the cursor again. Can anyone help point me in the right direction?
Here is my code:
 #pragma strict
 
 function Start () {
     Screen.lockCursor = true;
 }
 
 function Update () {
 
     Screen.lockCursor = true;
 
 }
 
 if(Input.GetButtonDown("Tab")) {
     Screen.lockCursor = false;
 }
 
               Thanks in Advance!
Answer by robertbu · Feb 23, 2013 at 09:59 PM
You write "button" but you use "Tab". Here is code for the tab key:
 function Start () {
     Screen.lockCursor = true;
 }
  
 function Update () {
     if(Input.GetKeyDown(KeyCode.Tab)) {
         Screen.lockCursor = false;
     }
     else if (Input.GetKeyUp(KeyCode.Tab)) {
         Screen.lockCursor = true;
     }
 }
 
              You could also use: if (Input.Get$$anonymous$$ey("tab") Screen.lockCursor = true; else Screen.lockCursor = false; 
Amazing dude, I think I will stick with your one, because it allows having to hold it down to release it, and I prefer that over other methods. But I upvoted the other one as well...
Answer by Coreyf716 · Feb 23, 2013 at 09:58 PM
The cursor is being locked every frame, therefore, when you try to unlock it, it is immediately locked again. Try this:
 function Start() {
      Screen.lockCursor = true;
 }
 
 function Update() {
      if (Input.GetKeyDown("tab"))
           Screen.lockCursor = !Screen.lockCursor;
 }
 
              This code doesn't seem to function correctly. Yes, it starts out locked, but it doesn't lock afterwards. But it seems functional, and so I learnt a bit from it. Thanks anyway, dude!
Your answer
 
             Follow this Question
Related Questions
A node in a childnode? 1 Answer
Touch Gui button 1 Answer
How do you collect a GameObject and make it Spawn somewhere else? 1 Answer