- Home /
Cursor is stuck and is not unlocking properly
Problem description: My cursor is still stuck in middle despite the fact that I unlock it in script. It is visible in middle though. Also whenever I press inventory button it goes back to middle. I have tried to disable looking code for debug purpose but it doesn't work.
Relevant snippets of code:
if (Input.GetKeyDown(KeyCode.I))//enable inventory script
{
openInventory = !openInventory;
}
if (openInventory == true)
{
ShowInventory();
foreach (Transform child in weaponRoot)
{
child.gameObject.SetActive(false);//no problem in here
}
//Cursor.lockState = CursorLockMode.Confined;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
else
{
HideInventory();
Cursor.lockState = CursorLockMode.Locked;
}
Looking script:
if (Input.GetKeyDown(KeyCode.Escape))
{
if (Cursor.lockState == CursorLockMode.Locked)
{
Cursor.lockState = CursorLockMode.None;
}
else
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
Dialogue with choices code snippet:
if (isTalking == true)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
else
{
Cursor.lockState = CursorLockMode.Locked;
}
Answer by Tkrain42 · Apr 14, 2020 at 06:31 AM
Cursor.lockState = CursorLockMode.Locked; will confine the cursor to the center of the game. When it is reset to CursorLockMode.None, it's at the center of the game to start, hence when you bring the inventory up, the cursor will be at the center of the game.
I suspect what's happening is you're continuing to set that lock state in Update() frame after frame... That might be forcing the cursor back to the center. Try to rework the game logic to only unlock or lock the cursor on the frame that the state changes.
Your answer

Follow this Question
Related Questions
whats wrong with my code? 1 Answer
error CS8025: Parsing error 1 Answer
lock camera to when open popup screen 3 Answers
Cursor.SetCursor not working in Unity 4.6 0 Answers
Centre cursor - Unity 5 (not locking or making invisible) 1 Answer