- Home /
Screen.CursorLock problems
Hey everyone!
I'm having a problem with hiding the cursor and locking it in the middle of the screen. It works at first but when I press esc it comes out of it and won't go back in.
Here's the code I am using to try and make this work :
void Update()
{
//Cursor Lock/Unlock//////////////////////////////////////////////////////////////////////////
if (Input.GetKeyDown(KeyCode.Escape) && cursorUnlocked == false)
{
cursorUnlocked = true;
Screen.showCursor = true;
Screen.lockCursor = false;
}
if (Input.GetKeyDown(KeyCode.Escape) && cursorUnlocked == true)
{
cursorUnlocked = false;
Screen.showCursor = false;
Screen.lockCursor = true;
}
/////////////////////////////////////////////////////////////////////////////////////////////
}
Thanks for your help guys!
Answer by Stabbarey · Mar 19, 2016 at 01:28 AM
Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false;
this is the correct code.
Answer by kevinrocks_786 · Mar 19, 2016 at 12:37 AM
Im pretty sure that Screen.lock or Screen.showCursor has been removed since Unity 5.0. However, What I use is this: http://docs.unity3d.com/ScriptReference/Cursor.html
So if you want to make the cursor visible/invisible, you can simply do:
Cursor.visible = true; // Or false
And to lock the cursor:
Cursor.lockState = CursorLockMode.Locked;
And to unlock it:
Cursor.lockState = CursorLockMode.None;
And take a look at Unity's Screen class. Notice how there are not cursor variables: http://docs.unity3d.com/ScriptReference/Screen.html
Both of you were right... Didn't know how to do it so I put a plus on you! Thanks for the help mate!
Your answer
![](https://koobas.hobune.stream/wayback/20220612072412im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Cursor Lockmode only works once? 1 Answer
Centre cursor - Unity 5 (not locking or making invisible) 1 Answer
Aimable reticle using mouse which has limited degrees 1 Answer