Search Unity

How to disable ESCAPE key to unlock the cursor. #FIX

Discussion in 'Scripting' started by anloko123, Jan 8, 2018.

  1. anloko123

    anloko123

    Joined:
    Dec 23, 2015
    Posts:
    65
    Hi, everybody says that Escape unlocks only in Editor... That's true, when you're using your own character controller scripts. If you're using the standard unity FPSController or TPController you can clearly see that escape key works in Standalone build too, i have a quick fix for that.
    - Locate "MouseLook.cs" in your assets folder.
    - Open "MouseLook.cs".
    The default "MouseLook.cs" look like this:
    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityStandardAssets.CrossPlatformInput;
    5.  
    6. namespace UnityStandardAssets.Characters.FirstPerson
    7. {
    8.     [Serializable]
    9.     public class MouseLook
    10.     {
    11.         public float XSensitivity = 2f;
    12.         public float YSensitivity = 2f;
    13.         public bool clampVerticalRotation = true;
    14.         public float MinimumX = -90F;
    15.         public float MaximumX = 90F;
    16.         public bool smooth;
    17.         public float smoothTime = 5f;
    18.         public bool lockCursor = true;
    19.  
    20.  
    21.         private Quaternion m_CharacterTargetRot;
    22.         private Quaternion m_CameraTargetRot;
    23.         private bool m_cursorIsLocked = true;
    24.  
    25.         public void Init(Transform character, Transform camera)
    26.         {
    27.             m_CharacterTargetRot = character.localRotation;
    28.             m_CameraTargetRot = camera.localRotation;
    29.         }
    30.  
    31.  
    32.         public void LookRotation(Transform character, Transform camera)
    33.         {
    34.             float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
    35.             float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;
    36.  
    37.             m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
    38.             m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
    39.  
    40.             if(clampVerticalRotation)
    41.                 m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);
    42.  
    43.             if(smooth)
    44.             {
    45.                 character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
    46.                     smoothTime * Time.deltaTime);
    47.                 camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
    48.                     smoothTime * Time.deltaTime);
    49.             }
    50.             else
    51.             {
    52.                 character.localRotation = m_CharacterTargetRot;
    53.                 camera.localRotation = m_CameraTargetRot;
    54.             }
    55.  
    56.             UpdateCursorLock();
    57.         }
    58.  
    59.         public void SetCursorLock(bool value)
    60.         {
    61.             lockCursor = value;
    62.             if(!lockCursor)
    63.             {//we force unlock the cursor if the user disable the cursor locking helper
    64.                 Cursor.lockState = CursorLockMode.None;
    65.                 Cursor.visible = true;
    66.             }
    67.         }
    68.  
    69.         public void UpdateCursorLock()
    70.         {
    71.             //if the user set "lockCursor" we check & properly lock the cursos
    72.             if (lockCursor)
    73.                 InternalLockUpdate();
    74.         }
    75.  
    76.         private void InternalLockUpdate()
    77.         {
    78.             if(Input.GetKeyUp(KeyCode.Escape))
    79.             {
    80.                 m_cursorIsLocked = false;
    81.             }
    82.             else if(Input.GetMouseButtonUp(0))
    83.             {
    84.                 m_cursorIsLocked = true;
    85.             }
    86.  
    87.             if (m_cursorIsLocked)
    88.             {
    89.                 Cursor.lockState = CursorLockMode.Locked;
    90.                 Cursor.visible = false;
    91.             }
    92.             else if (!m_cursorIsLocked)
    93.             {
    94.                 Cursor.lockState = CursorLockMode.None;
    95.                 Cursor.visible = true;
    96.             }
    97.         }
    98.  
    99.         Quaternion ClampRotationAroundXAxis(Quaternion q)
    100.         {
    101.             q.x /= q.w;
    102.             q.y /= q.w;
    103.             q.z /= q.w;
    104.             q.w = 1.0f;
    105.  
    106.             float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
    107.  
    108.             angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
    109.  
    110.             q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
    111.  
    112.             return q;
    113.         }
    114.  
    115.     }
    116. }
    Look at this part of the code:
    Code (CSharp):
    1.  private void InternalLockUpdate()
    2.         {
    3.          
    4.  private void InternalLockUpdate()
    5.         {
    6.             if(Input.GetKeyUp(KeyCode.Escape))
    7.             {
    8.                 m_cursorIsLocked = false;
    9.             }
    10.  private void InternalLockUpdate()
    11.         {
    12.             if(Input.GetKeyUp(KeyCode.Escape))
    13.             {
    14.                 m_cursorIsLocked = false;
    15.             }
    16.  
    Just delete:
    Code (CSharp):
    1. if(Input.GetKeyUp(KeyCode.Escape))
    2.             {
    3.                 m_cursorIsLocked = false;
    4.             }
    And your code should look like this:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityStandardAssets.CrossPlatformInput;
    4.  
    5. namespace UnityStandardAssets.Characters.FirstPerson
    6. {
    7.     [Serializable]
    8.     public class MouseLook
    9.     {
    10.         public float XSensitivity = 2f;
    11.         public float YSensitivity = 2f;
    12.         public bool clampVerticalRotation = true;
    13.         public float MinimumX = -90F;
    14.         public float MaximumX = 90F;
    15.         public bool smooth;
    16.         public float smoothTime = 5f;
    17.         public bool lockCursor = true;
    18.  
    19.  
    20.         private Quaternion m_CharacterTargetRot;
    21.         private Quaternion m_CameraTargetRot;
    22.         private bool m_cursorIsLocked = true;
    23.  
    24.         public void Init(Transform character, Transform camera)
    25.         {
    26.             m_CharacterTargetRot = character.localRotation;
    27.             m_CameraTargetRot = camera.localRotation;
    28.         }
    29.  
    30.  
    31.         public void LookRotation(Transform character, Transform camera)
    32.         {
    33.             float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
    34.             float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;
    35.  
    36.             m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
    37.             m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
    38.  
    39.             if(clampVerticalRotation)
    40.                 m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);
    41.  
    42.             if(smooth)
    43.             {
    44.                 character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
    45.                     smoothTime * Time.deltaTime);
    46.                 camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
    47.                     smoothTime * Time.deltaTime);
    48.             }
    49.             else
    50.             {
    51.                 character.localRotation = m_CharacterTargetRot;
    52.                 camera.localRotation = m_CameraTargetRot;
    53.             }
    54.  
    55.             UpdateCursorLock();
    56.         }
    57.  
    58.         public void SetCursorLock(bool value)
    59.         {
    60.             lockCursor = value;
    61.             if(!lockCursor)
    62.             {//we force unlock the cursor if the user disable the cursor locking helper
    63.                 Cursor.lockState = CursorLockMode.None;
    64.                 Cursor.visible = true;
    65.             }
    66.         }
    67.  
    68.         public void UpdateCursorLock()
    69.         {
    70.             //if the user set "lockCursor" we check & properly lock the cursos
    71.             if (lockCursor)
    72.                 InternalLockUpdate();
    73.         }
    74.  
    75.         private void InternalLockUpdate()
    76.         {
    77.             else if(Input.GetMouseButtonUp(0))
    78.             {
    79.                 m_cursorIsLocked = true;
    80.             }
    81.  
    82.             if (m_cursorIsLocked)
    83.             {
    84.                 Cursor.lockState = CursorLockMode.Locked;
    85.                 Cursor.visible = false;
    86.             }
    87.             else if (!m_cursorIsLocked)
    88.             {
    89.                 Cursor.lockState = CursorLockMode.None;
    90.                 Cursor.visible = true;
    91.             }
    92.         }
    93.  
    94.         Quaternion ClampRotationAroundXAxis(Quaternion q)
    95.         {
    96.             q.x /= q.w;
    97.             q.y /= q.w;
    98.             q.z /= q.w;
    99.             q.w = 1.0f;
    100.  
    101.             float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
    102.  
    103.             angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
    104.  
    105.             q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
    106.  
    107.             return q;
    108.         }
    109.  
    110.     }
    111. }
    I hope it works, Happy New Year for everyone!
    Follow my games on GameJolt:
    https://gamejolt.com/games/darkness/298431



    P.S. Sorry for my bad english, im actually from Poland :).
     
    Last edited: Sep 27, 2018
    xabiere likes this.
  2. xabiere

    xabiere

    Joined:
    Nov 11, 2017
    Posts:
    1
    Thank you so much! my savior!
     
    anloko123 likes this.
  3. Legion4

    Legion4

    Joined:
    Apr 18, 2017
    Posts:
    4
    Was searching everywhere for this answer
     
    anloko123 likes this.
  4. allakazan5555

    allakazan5555

    Joined:
    Mar 17, 2018
    Posts:
    10
    Thanks, that's the only response that actually works well
     
    anloko123 likes this.
  5. field-os

    field-os

    Joined:
    Nov 10, 2018
    Posts:
    1
    This doesn't work for me. The mouse is still visible, but won't move.

    EDIT: Fixed it by changing nothing. One of those things I guess.
     
    anloko123 likes this.
  6. tito91

    tito91

    Joined:
    Aug 30, 2015
    Posts:
    10
    Hello, everyone. Does anybody knows if this default Unity behaviour making cursor visible when Escape is pressed can be disabled? It's kind of annoying for me, I would rather have cursor enabled with other key, even in Editor.
     
  7. DZeb

    DZeb

    Joined:
    Sep 17, 2015
    Posts:
    5
    To add to the original post, if you still want that behavior in editor only you could just wrap that part of the code with a preprocessor directive, such as #if UNITY_EDITOR. You can do this inside the InternalLockUpdate() function of the MouseLook.cs like this:
    Code (CSharp):
    1.         private void InternalLockUpdate()
    2.         {
    3. #if UNITY_EDITOR
    4.             if (Input.GetKeyUp(KeyCode.Escape))
    5.             {
    6.                 m_cursorIsLocked = false;
    7.             }
    8.             else if (Input.GetMouseButtonUp(0))
    9.             {
    10.                 m_cursorIsLocked = true;
    11.             }
    12. #endif
    13.             if (m_cursorIsLocked)
    14.             {
    15.                 Cursor.lockState = CursorLockMode.Locked;
    16.                 Cursor.visible = false;
    17.             }
    18.             else if (!m_cursorIsLocked)
    19.             {
    20.                 Cursor.lockState = CursorLockMode.None;
    21.                 Cursor.visible = true;
    22.             }
    23.         }