So I wrote a movement script that includes a cursor lock and hide function. The problem is, is that when I unlock the cursor with the Escape key, the cursor can freely move around. That is what I wanted it to do. When I re-lock the cursor, it disappears ONLY when it is on the game screen, but it can still freely move around. When it exits the game frame, it shows. It does not lock on re-lock. How would I fix this?
private bool isMouseLocked = true;
void Update () {
if (Input.GetButtonDown ("Unlock")) {
isMouseLocked = !isMouseLocked;
}
if (isMouseLocked == true) {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
if (isMouseLocked == false) {
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}