Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Some problem with cursor

Discussion in 'Scripting' started by Ksaneko, Jul 28, 2018.

  1. Ksaneko

    Ksaneko

    Joined:
    Jan 2, 2016
    Posts:
    7
    Hello friends.
    In my project i need a lock cursor (centre).
    I used that code in Update of standart FPS controller script (i deleted the same code in MouseLook):
    Cursor.visible = true;
    Cursor.lockState = CursorLockMode.Locked;
    Screen.lockCursor = true;

    But when i switch application (alt+tab for example) and then switch to my game, cursor not at centre. But still not visible. How can i fix it? Tnx.
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Have you tried taking a look into moving that code into an application focus handler? That may help.
     
  3. Ksaneko

    Ksaneko

    Joined:
    Jan 2, 2016
    Posts:
    7
    Idk how OnApplicationFocus should help to my problem( As I understand this method only informs about the change of the active application. I tried to apply the method, but with alt + tab the cursor center is still shifted (
    Cursor.lockState = CursorLockMode.Locked and Screen.lockCursor = true still do not worked.

    Code (CSharp):
    1. public class AppPause : MonoBehaviour {
    2.  
    3.     bool isPaused = false;
    4.  
    5.     void OnApplicationFocus(bool hasFocus)
    6.     {
    7.         isPaused = !hasFocus;
    8.     }
    9.  
    10.     void OnApplicationPause(bool pauseStatus)
    11.     {
    12.         isPaused = pauseStatus;
    13.     }
    14.    
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.  
    19.         if (isPaused)
    20.         {
    21.             Cursor.visible = true;
    22.             Cursor.lockState = CursorLockMode.None;
    23.             Screen.lockCursor = false;
    24.         }
    25.         else
    26.         {
    27.             Cursor.visible = false;
    28.             Cursor.lockState = CursorLockMode.Locked;
    29.             Screen.lockCursor = true;
    30.         }
    31.     }
    32. }
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I have amended the code only slightly, attached it to the Camera, and it appears to be working for me. If you try it do you see the debug logs printed out in the console? :
    Code (CSharp):
    1. public class AppPause : MonoBehaviour
    2. {
    3.     void OnApplicationFocus(bool hasFocus)
    4.     {
    5.         ControlCursor( !hasFocus );
    6.     }
    7.  
    8.     void OnApplicationPause(bool pauseStatus)
    9.     {
    10.         ControlCursor( pauseStatus );
    11.     }
    12.  
    13.     void ControlCursor(bool isPaused)
    14.     {
    15.         if (isPaused)
    16.         {
    17.             Debug.Log("PAUSED");
    18.             Cursor.visible = true;
    19.             Cursor.lockState = CursorLockMode.None;
    20.         }
    21.         else
    22.         {
    23.             Debug.Log("play mode");
    24.             Cursor.visible = false;
    25.             Cursor.lockState = CursorLockMode.Locked;
    26.         }
    27.     }
    28. }
     
  5. Ksaneko

    Ksaneko

    Joined:
    Jan 2, 2016
    Posts:
    7
    IDK why, but when i replaced code from Update, everything worked 0_0
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    The only difference was removing the calls to
    Screen.lockCursor = <bool>;
    as they are deprecated.