Search Unity

Two problems related to Cursor.lockState in Linux editor. 5.4.0p1, in case still present in 5.5

Discussion in 'Linux' started by Lao2211, Oct 5, 2016.

  1. Lao2211

    Lao2211

    Joined:
    Apr 6, 2016
    Posts:
    3
    Ubuntu 16.04 64 bits with NVidia GT630 driver version 361.42.

    1-
    In the release build (not in the editor preview) when setting Cursor.lockState to CursorLockMode.Locked and then back to CursorLockMode.None, the system cursor is not shown again, making impossible for the player to see where the cursor is. By try and error the game exit button can be clicked (you move the cursor until the button is highlighted).

    2-
    In the editor, when using a maximized preview, setting Cursor.lockState to CursorLockMode.Locked hides the system cursor but it looks like it fails to maintain the cursor at the center of the preview area where the game is running. I know this because when setting it back to CursorLockMode.None the cursor isn't made visible at the center of the preview area but at a random coordinate on the screen, like it was not hold at the center while in locked state.

    This causes problems in the game preview, because the camera control script fails to rotate the camera when you reach the border of the screen with the invisible cursor (another indication of it not being hold at the center). I thought it was a bug in my code but I tried it in a release build and it behaved correctly so I started to try random things in the editor and found this.

    Possible workaround for both are: using a custom cursor with custom draw code, this should immunize again the cursor not being shown again. Manually resetting the cursor position to the center of the screen when you are sure that you don't need to check the "Mouse X" and "Mouse Y" axes again.
     
  2. Tak

    Tak

    Joined:
    Mar 8, 2010
    Posts:
    1,001
    The best way to go about this is to set the cursor visibility at the same time that you set the cursor state.

    Code (csharp):
    1. Cursor.visible = false;
    2. Cursor.lockState = CursorLockMode.Locked;
    Code (csharp):
    1. Cursor.lockState = CursorLockMode.None;
    2. Cursor.visible = true;
     
  3. Lao2211

    Lao2211

    Joined:
    Apr 6, 2016
    Posts:
    3
    Thank you, it works perfectly.

    A minor modification: when setting Cursor.lockState = CurosLockMode.Locked, the mouse is hidden automatically, so no need to set Cusor.visible = false at the same time, but later when freeing the mouse again, I'm doing this:

    When freeing the mouse again (player pressed ESC and main menu is shown):

    Code (csharp):
    1.  
    2. Cursor.lockState = CursorLockMode.Node;
    3.  
    4. // Fix for system cursor not showing again
    5. Cursor.visible = false;
    6. Cursor.visible = true;
    7.  
    I think is more a system bug than a Unity problem. Whatever may be happening, manually setting Cursor.visible is working so far.

    The other problem remains. When running the game in preview mode inside editor, the cursor locking doesn't behave as expected. It seems that there is no multiplatform way of setting the cursor coordinates. This answer: http://answers.unity3d.com/questions/9408/set-cursor-position.html suggests that at least in Windows it can be done using System.Windows.Form.Cursor.Position but, and correct me if I'm wrong, you cannot use System.Windows.Form under Mono in Linux. This seems to happen only in editor, the release build of the game works correctly.
     
  4. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
  5. Lao2211

    Lao2211

    Joined:
    Apr 6, 2016
    Posts:
    3
    I found that post looking for a solution.

    I finally updated my Unity editor. It seems solved in 5.5.0xb5. Also OpenGL version used is now 4.5 :)

    Now the preview area can't be maximized, but if I have to chose between that and before, I prefer the current version. I was using a small preview area anyway, to avoid issues with the camera.