- Home /
Screen.lockCursor leaves mouse where it was
I have this code inside my Update function that locks the cursor at the center of the screen when I press play because CanLook is true by default. Then when I press escape the cursor shows up. The problem is that when the game is running it doesn't lock the cursor anymore. It just hides it.
if (Input.GetKeyDown ("escape")) {
CanLook = !CanLook;
}
Screen.lockCursor = CanLook;
When I press escape to lock it again and move the mouse if I unlock it the mouse is not center. Can anyone tell my when Screen.lockCursor isn't centering or locking the mouse. It does it the first time I press play.
UPDATE: I found out that if I click before unlocking it sets it to the center. So every time I click it gets centered. I am very confused
I answered my own question
Don't use escape to unlock the screen
Answer by ScaniX · Jul 26, 2016 at 09:14 AM
Although you "answered your own question" I will just note that this behaves different in the editor than in the real game in the end. It makes sense as you somehow need a way to break free to use your mouse in the inspector.
Otherwise it would be too easy to lock yourself out by locking the mouse in. :)
As you already found out: By clicking in the game view, Unity returns the control of the mouse to the game.
Yes, I found out that escape is the key used so for testing purposes I recommend using a different key so that you know exactly how the game is acting. For now I am using tab
Answer by SorenaCoder · Jul 26, 2016 at 09:29 PM
you must use Screen.lockCursor in OnGUI function not in update
Your answer
Follow this Question
Related Questions
Smoother Rotation with mouse 2 Answers
First Person Controller MouseLook on Android 2 Answers
Gameobject follow mouse 0 Answers
[SOLVED] Raycast only hits one side of the box collider 1 Answer
Multiple Cars not working 1 Answer