Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Zeriver · May 30, 2018 at 08:37 PM · mousecursorlockminimize

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Loui_Studios · Jun 03, 2018 at 03:57 PM 0
Share

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;

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zeriver · Jul 21, 2018 at 05:08 PM 0
Share

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!

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zeriver · May 31, 2018 at 09:59 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

96 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges