Search Unity

Cursor.lockState = CursorLockMode.Locked; [SOLVED]

Discussion in 'Scripting' started by ChuckieGreen, Apr 7, 2018.

  1. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    I have a pause menu, and when the esc button is pressed the paused menu shows, and when esc is pressed again it disappears. So when it shows the cursor shows and is also unlocked. and when it goes back to the game the cursor is invisible and is meant to be locked. But The cursor keeps going onto my left and right screens (Unity on middle screen). This is the code

    Code (CSharp):
    1.  void PauseGame(bool state)
    2.     {
    3.         if(state)
    4.         {
    5.             Time.timeScale = 0.0f;
    6.             camMouseLook.enabled = false;
    7.             smgController.enabled = false;
    8.             Cursor.visible = true;
    9.             Cursor.lockState = CursorLockMode.None;
    10.         }
    11.         else
    12.         {
    13.             Time.timeScale = 1.0f;
    14.             camMouseLook.enabled = true;
    15.             smgController.enabled = true;
    16.             Cursor.visible = false;
    17.             Cursor.lockState = CursorLockMode.Locked;
    18.             Debug.Log("Cursor locked");
    19.         }
    20.         pauseMenu.SetActive(state);
    21.     }
    The debug for cursor locked is showing in the log, so that part is running. Its just not locking. Does anyone possibly have an idea why. Appreciate any help
     
    cody01100011 likes this.
  2. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    For anyone having the same issue, the locked cursor state works when the game is built. Only seems to be an issue when playing the game in unity itself
     
    ravisutkanit and KUFgoddess like this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I think it's a fool proofing in case you forget to unlock it in your code.
    The other option is killing unity with task manager and losing all unsaved changes
     
  4. Chris_Payne_QS

    Chris_Payne_QS

    Joined:
    Jul 16, 2016
    Posts:
    84
    That's not solved at all then.
    I've just upgraded my own project to 2018.1 and now I can't playtest it in the editor because the cursor escapes. Why has this changed in 2018.1 and what is the workaround?
    It seems unlikely to be a safety precaution because a) it was safe before, unlocking the cursor when the game crashed and b) it now prevents ALL mouse-driven games being tested properly.
     
  5. Leniaal

    Leniaal

    Joined:
    Nov 7, 2012
    Posts:
    119
    For me my cursor freezes at the center of the screen in editor mode, however it works properly when I build my game. Not sure what Unity changed but I can't imagine it's really intended.
    #if UNITY_EDITOR seems to be the solution for now, though I'm not really happy with it.
     
  6. muzboz

    muzboz

    Joined:
    Aug 8, 2012
    Posts:
    110
    (Using Unity 2018.2.0b9)
    Keen to hear a good solution for this. It's pretty annoying.
    It worked fine in past versions of Unity, so I don't understand why it should be implemented differently now for any reason.

    If I'm using the Editor, and I click back in the game window, I want the cursor to be properly Locked there.
    Currently, the cursor sways out onto other screens, thus breaking out of the game when left and right clicking "in-game".
     
  7. muzboz

    muzboz

    Joined:
    Aug 8, 2012
    Posts:
    110
    @Leniaal

    What do you mean by "#if UNITY_EDITOR seems to be the solution for now" ...?

    What is it that you are doing with that?
     
  8. Leniaal

    Leniaal

    Joined:
    Nov 7, 2012
    Posts:
    119
    @muzboz

    Code (CSharp):
    1. #if UNITY_EDITOR
    2.         Cursor.lockState = CursorLockMode.None;
    3.         Cursor.visible = true;
    4. #else
    5.         Cursor.lockState = CursorLockMode.None;
    6.         Cursor.lockState = CursorLockMode.Confined;
    7.         Cursor.visible = true;
    8. #endif
     
  9. muzboz

    muzboz

    Joined:
    Aug 8, 2012
    Posts:
    110
    I put this in the Start() function of a script, and it worked how I wanted it to, in Editor mode, and in the Standalone build on Windows. So I'm happy for now! :) Thanks @Leniaal ...

    Code (CSharp):
    1. #if UNITY_EDITOR
    2. Cursor.lockState = CursorLockMode.None;
    3. Cursor.visible = true;
    4. #endif
    5.  
    6. #if UNITY_STANDALONE
    7. Cursor.lockState = CursorLockMode.Locked;
    8. Cursor.visible = false;
    9. #endif
     
  10. Kenthria11

    Kenthria11

    Joined:
    May 6, 2017
    Posts:
    20
    Code (CSharp):
    1. void Start()
    2.     {
    3.         #if UNITY_EDITOR
    4.                 Cursor.lockState = CursorLockMode.None;
    5.                 Cursor.visible = true;
    6.         #else
    7.                 Cursor.lockState = CursorLockMode.None;
    8.                 Cursor.lockState = CursorLockMode.Confined;
    9.                 Cursor.visible = true;
    10.         #endif
    No errors but doesnt do a thing in the editor for me.
     
    JoeStrout and Xander-Davis like this.
  11. cody01100011

    cody01100011

    Joined:
    May 21, 2020
    Posts:
    4
    Code (CSharp):
    1. void Start()
    2.     {
    3.       Cursor.lockState = CursorLockMode.Locked; // Defaults cursor invisible
    4.     }
    5. void Update()
    6.    {
    7.      Cursor.visible = true; // Defaults, if press left mouse, cursor reset to center (❤ ω ❤)
    8.    }
     
    Last edited: Aug 18, 2021
    blackcoffee1 likes this.