- Home /
Cursor lock not working after alt tab
Hi,
I am locking cursor in my game like this:
if (value)
{
Cursor.lockState = CursorLockMode.Confined;
}
else
{
Cursor.lockState = CursorLockMode.None;
}
And it works fine, prevents mouse from going to my second monitor and i can disable the lock at any time in options. But when I alt-tab or just when app loses focus, then the cursor lock doesn't work after coming back to the app. Even setting lock again in the options doesn't help.
Would really appreciate help with this problem and thanks for your help in advance!
Been having exact same problem for months! When I lose focus the cursor no longer locks. In builds it does lock but only while the mouse button is held down. When I release, the cursor is unlocked again.
if (lockCursor) {
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
}
else {
Cursor.lockState = CursorLock$$anonymous$$ode.None;
}
Cursor.visible = !lockCursor;
Answer by andy_ghe97 · Jul 09, 2018 at 07:38 PM
"Found out the problem was being caused by the "Run in background*" player setting being disabled. Enabling it has fixed the problem!" -from https://www.reddit.com/r/Unity3D/comments/8nxk76/cursor_lock_commands_are_broken_in_builds_made/
The answer : https://docs.unity3d.com/Manual/class-PlayerSettingsStandalone.html @Zeriver
Thank you! That pretty much solved the problem. It's weird but it works... well, what else is new, eh? Anyway thanks m8, really appreciate your help!
Answer by b1gry4n · May 30, 2018 at 10:56 PM
try using https://docs.unity3d.com/ScriptReference/Application-isFocused.html as your bool check. if the application is focused, lock cursor, else dont
Thanks for the answer but the problem seems to be that after alt-tabbing the game, the CursorLock$$anonymous$$ode.Confined; command is not working. Cursor can still go to the other screen despite the fact that it's supposed to be confined.
Your answer
Follow this Question
Related Questions
Screen.lockCursor messes my rotation 1 Answer
Locking cursor/mouse on an object 1 Answer
Hide and lock the mouse cursor (beginner) 0 Answers
How to lock mouse? 1 Answer
Locking the mouse cursor without it centering after unlocking it? 0 Answers