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

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. :slight_smile:

As you already found out: By clicking in the game view, Unity returns the control of the mouse to the game.

you must use Screen.lockCursor in OnGUI function not in update