- Home /
Mouse won't lock
HI! :D im new here and still learning UnityScript. But i have a Problem. I have a script that Pause the game and Unlock the Mouse. But when i Resume. The Mouse is still Unlocked! any Ideas?
Script:
private var pauseEnabled = false;
function Start(){
pauseEnabled = false;
Screen.lockCursor = true;
Screen.showCursor = false;
}
function Update(){
if(Input.GetKeyDown("escape")){
if(pauseEnabled == true){
pauseEnabled = false;
Screen.lockCursor = true;
Screen.showCursor = false;
}else{
if(pauseEnabled == false){
pauseEnabled = true;
Screen.lockCursor = false;
Screen.showCursor = true;
}
}
}
}
Answer by SomeGuy22 · Jul 26, 2012 at 08:34 PM
It is unwise to use escape as the lock and unlock key.
By default, the Unity Editor and Webplayer both UNLOCK the cursor when you press escape, so no one can trick an online player into having their mouse permanently stuck.
In a standalone build, this script will work fine, but it is still not recommended.
Instead, you should use mouse click to re-lock the cursor.
How would you go about making a mouse click re-lock the cursor then?
if (Input.GetButtonDown("Fire1") && !Screen.lockCursor) Screen.lockCursor = true;
However if you want to be more professional it's probably best to use a GUI Button to do this ins$$anonymous$$d:
if (GUI.Button(Button rectangles and stuff) && !Screen.lockCursor) Screen.lockCursor = true;
Your answer
![](https://koobas.hobune.stream/wayback/20220613074740im_/https://answers.unity.com/themes/thub/images/avi.jpg)