Search Unity

Mouse cursor locked in centre, but still visible (build only)

Discussion in 'Editor & General Support' started by BabylonCandle, Feb 10, 2018.

  1. BabylonCandle

    BabylonCandle

    Joined:
    Feb 8, 2018
    Posts:
    2
    If anyone could help with this it would be much appreciated. I've searched the net for hours looking at every article and post on unity mouse cursors I could find, I've found nothing to help solve this.

    The Problem:
    If I run my game in the editor the mouse pointer is locked and hidden exactly as it should. I unlock it in the pause menu, and it locks and hides again when I continue. All fine, and exactly as it should be.
    But, when I make a build and run it (standalone, not in the editor) the mouse pointer is locked in the centre of the screen, but it's still visible.

    I created the simplest scene possible to test this. All I have in it is the following script attached to an empty game object:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MouseTest : MonoBehaviour {
    4.  
    5.     void Start () {
    6.         Cursor.lockState = CursorLockMode.Locked;
    7.     }
    8.  
    9.     void Update () {
    10.  
    11.     if(Input.GetKeyDown(KeyCode.L))
    12.         Cursor.lockState = CursorLockMode.Locked;
    13.  
    14.     if (Input.GetKeyDown(KeyCode.U))
    15.         Cursor.lockState = CursorLockMode.None;
    16.     }
    17. }
    This works in the editor: Starts with the cursor locked and not visible. hit "U" key, is shows and I can move it around. Hit "L" key, it's hidden and locked again. All fine.
    Make a build. Starts with the cursor locked in the centre of the screen. But it's still visible, just can't move it.

    Anyone know a fix for this?
     
    Bip901 likes this.
  2. BabylonCandle

    BabylonCandle

    Joined:
    Feb 8, 2018
    Posts:
    2
    I have a workaround for anyone else that gets the same issue.

    Even though the unity documentation for Cursor.Lockstate reads:
    "When Locked, the cursor is placed in the center of the view and cannot be moved. The cursor is invisible in this state, regardless of the value of Cursor.visible."

    This is true in the editor, but not in builds (for me at least).

    If I also change the Cursor.Visible it works around this. So if the code above is changed like this, it works both in the editor and in build.

    Code (CSharp):
    1.         if (Input.GetKeyDown(KeyCode.L)) {
    2.             Cursor.lockState = CursorLockMode.Locked;
    3.             Cursor.visible = false;
    4.         }
    5.         if (Input.GetKeyDown(KeyCode.U)) {
    6.             Cursor.lockState = CursorLockMode.None;
    7.             Cursor.visible = true;
    8.         }
     
  3. mystman12

    mystman12

    Joined:
    Jul 20, 2017
    Posts:
    3
    I'm having the same issue, glad to know there's an easy workaround though, thanks. I'm guessing it's a bug or something.
     
  4. Chris_Payne_QS

    Chris_Payne_QS

    Joined:
    Jul 16, 2016
    Posts:
    84
    This is exactly what I was already doing. But since upgrading to 2018.1 the cursor is no longer locked when I set it into the locked state. It's invisible but can escape onto the editor panels, making the game unplayable.
     
  5. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    292
    Last edited: Apr 4, 2019
  6. Mossab95

    Mossab95

    Joined:
    Sep 25, 2018
    Posts:
    1
    I just wanna say thanks so so much guys for the help i swear i have been locking for solutions regarding this for almost a weeks and couldnt find a way to solve the invisible cursor but now i managed and all thanks to you guys. Finally i can finish up my game and submit my final project
     
  7. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    197
    helped me. thanks. cant believe this kind of stuff happens with this engine.

    i will note that i noticed the mouse cursor for me does hide if i alt tab into another window and then click back into the game. weird.
     
  8. rameez_safdar

    rameez_safdar

    Joined:
    Feb 18, 2020
    Posts:
    14
    Thanks. Helped me
     
  9. RiteshAmbastha

    RiteshAmbastha

    Joined:
    May 3, 2020
    Posts:
    1
    Thanks for the solution
     
  10. PrasadPDC

    PrasadPDC

    Joined:
    Jan 26, 2020
    Posts:
    3
    thank you so much you save my life