Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Cursor Lockstate Troubles

Discussion in 'Unity 5 Pre-order Beta' started by LudiKha, Nov 5, 2014.

  1. LudiKha

    LudiKha

    Joined:
    Feb 15, 2014
    Posts:
    140
    Has anybody so far succesfully used cursor lockstate and visibility in Unity 5? For me, it doesn't seem to work reliably. The code I use is:

    Code (CSharp):
    1. Cursor.visible = false;
    2. Cursor.lockState = CursorLockMode.Locked;
     
  2. terravires

    terravires

    Joined:
    Mar 27, 2013
    Posts:
    103
    I've already reported this bug, they confirmed and said it will be fixed... but couldn't tell me when or what version. o_O

    I tested and reported with b11 btw.
     
  3. LudiKha

    LudiKha

    Joined:
    Feb 15, 2014
    Posts:
    140
    Okay, cheers!
     
  4. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    What exactly was that bug about?

    I was fighting with the cursor lock and finally came up with this piece of code:

    Code (csharp):
    1. private void Update()
    2. {
    3.    if (cursorShouldBeLocked)
    4.    {
    5.      Cursor.lockState = CursorLockMode.Locked;
    6.      Cursor.visible = false;
    7.    }
    8.    else
    9.    {
    10.      Cursor.lockState = CursorLockMode.None;
    11.      Cursor.visible = true;
    12.    }
    13. }
     
  5. RElam

    RElam

    Joined:
    Nov 16, 2009
    Posts:
    375
    Yea, this is still broken in B18. Alexzzzz's solution kinda works in editor, but mouse position seems to be wrong as clicking loses focus on game window and it doesn't work outside editor. I'd also wager it'd be bad if this had to be done every update, certainly shouldn't need to be and didn't need to be previous to U5.
     
  6. terravires

    terravires

    Joined:
    Mar 27, 2013
    Posts:
    103
    That does not fix the problem. The editor often un-hides the cursor on it's own and even using that code once per frame it flickers back over my screen. I've noticed the the cursor resets position to the Scene View editor on my monitor #1, while my game view is on monitor #2. It's very broken, even not coming back after setting Cursor.visible = true and lock state to none. As I said, I reported back on b11 some time ago. And they said they were going to fix it, but not when. Sad that it still exists two months later.

    As another note, the first time you do it seems to stay hidden/locked. However, after a few toggles it breaks and internal state seems corrupt/out of sync.
     
  7. milox777

    milox777

    Joined:
    Sep 23, 2012
    Posts:
    195
    Still not working properly as of b21 - it's really annoying, it's clicking anywehere randomly, going off screen
     
  8. RElam

    RElam

    Joined:
    Nov 16, 2009
    Posts:
    375
    Yea, I noticed that setting the lock state property, even if it's not changing, will change the cursor focus to a new window. Seemingly, there's some code run in that property setter that's likely looking at the last drawn window and locking to that, since it seemingly will jump to a window that redraws per frame (for me, profiler and animator, never project or inspector). They really need to fix this though, it's pretty damn crippling for testing my project.
     
  9. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I no longer see the cursor flickering in b22. I'm still using the code from my earlier post. I worked fine in standalone builds, but there was flickering in the editor. Now everything looks fine.
     
  10. Kubold

    Kubold

    Joined:
    May 10, 2012
    Posts:
    359
    RC2, cursor still flickers :/
     
  11. koboldskeep

    koboldskeep

    Joined:
    Dec 24, 2012
    Posts:
    7
    I am also getting this bug in 5.0.0b22 Pro. terravires, can you tell me what was the bug number?
     
  12. Kubold

    Kubold

    Joined:
    May 10, 2012
    Posts:
    359
    I managed to get around it in cooked build. I set the cursor texture as a totally transparent GUI texture. Even if it flickers, it's invisible anyway.
     
  13. SteveSwink

    SteveSwink

    Joined:
    Aug 25, 2012
    Posts:
    23
    Has there been any progress on this? Someone had submitted a ticket for it, right?

    I'm setting locked or unlocked every frame since that's the only way to get remotely playable behavior but the cursor is flickering and sometimes gets locked at the lower left of the screen instead of the center. Arg!
     
  14. BigB

    BigB

    Joined:
    Oct 16, 2008
    Posts:
    672
    I did submit a ticket, but they replied that they can't reproduce it, is this thing still in RC3 ?
     
  15. SteveSwink

    SteveSwink

    Joined:
    Aug 25, 2012
    Posts:
    23
    Yeah, I still see it. Lack of repro is crazy - it so obviously doesn't work at all :(
     
  16. SteveSwink

    SteveSwink

    Joined:
    Aug 25, 2012
    Posts:
    23
    What was your repro case? I see it break just by toggling Locked / None using GetKeyDown
     
  17. BigB

    BigB

    Joined:
    Oct 16, 2008
    Posts:
    672
    I didn't had a repro case, this was what I sent them :

    1) What happened

    This code :

    Cursor.lockState = CursorLockMode.Locked;
    Cursor.lockState = CursorLockMode.None;

    Do not work.
    Sometimes they do, most of the times they don't.
    It's impossible to center the mouse in the screen, and unlock it to go to a menu, and lock it again to go back to the game.

    See aditional posts by other users :

    http://forum.unity3d.com/threads/locking-the-mouse.294453/#post-1941423

    http://forum.unity3d.com/threads/cursor-lockstate-troubles.278221/
     
    Snikard likes this.