Search Unity

Mouse Cursor Issue

Discussion in 'Scripting' started by SprinkledSpooks, Nov 23, 2016.

  1. SprinkledSpooks

    SprinkledSpooks

    Joined:
    Jun 1, 2016
    Posts:
    117
    I'm currently attempting to write a script that, when a certain UI element is activated, it will allow the player to press the 'E' key to switch cameras to a camera named cam2. So far I am successful, except for one aspect:

    The cursor does not show up. I believe that this is due to the fact that the camera is switching from the default FPSController because, when I disable the First Person Controller script in edit mode, the cursor will show up. I tried adding the line
    Code (CSharp):
    1. Cursor.visible = true;
    at the end of the IF statement. By using Debug.Log, I have confirmed that the line of code is executing successfully, but the mouse cursor doesn't appear to actually be appearing once again. I've even attempted to deactivate the FPSController entirely, which I have successfully done so, but the mouse cursor remains invisible. Here's my complete code below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CameraSwitch : MonoBehaviour {
    7.     public Text activateText;
    8.     public GameObject Jim;
    9.     public Camera cam1;
    10.     public Camera cam2;
    11.     public GameObject player;
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.         cam1.enabled = true;
    17.         cam2.enabled = false;
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update ()
    22.     {
    23.         if (Input.GetKeyDown("e"))
    24.         {
    25.             Debug.Log("E Pressed!");
    26.             if (activateText.enabled == true)
    27.             {
    28.                 cam1.enabled = !cam1.enabled;
    29.                 cam2.enabled = !cam2.enabled;
    30.                 player.SetActive(true);
    31.  
    32.                 if (cam2.enabled == true)
    33.                 {
    34.                     Cursor.visible = true;
    35.                     Debug.Log("Cursor should be enabled!");
    36.                     player.SetActive(false);
    37.                 }
    38.             }
    39.         }
    40.     }
    41. }
    42.  
    Any ideas on how to fix this?
     
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    432
    What is the code that hides the cursor first? Any other occurences in code for cursor?
     
  3. Aberdyne

    Aberdyne

    Joined:
    Mar 17, 2015
    Posts:
    64
    Just throwing what comes to mind... Have you tried that?
    Cursor.visible = false;
    Cursor.visible = true;
     
  4. SprinkledSpooks

    SprinkledSpooks

    Joined:
    Jun 1, 2016
    Posts:
    117
    Yeah, hasn't worked though.
     
  5. SprinkledSpooks

    SprinkledSpooks

    Joined:
    Jun 1, 2016
    Posts:
    117
    Here's the script that I believe is causing the issue:
    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.             if(Input.GetKeyUp(KeyCode.Escape))
    78.             {
    79.                 m_cursorIsLocked = false;
    80.             }
    81.             else if(Input.GetMouseButtonUp(0))
    82.             {
    83.                 m_cursorIsLocked = true;
    84.             }
    85.  
    86.             if (m_cursorIsLocked)
    87.             {
    88.                 Cursor.lockState = CursorLockMode.Locked;
    89.                 Cursor.visible = false;
    90.             }
    91.             else if (!m_cursorIsLocked)
    92.             {
    93.                 Cursor.lockState = CursorLockMode.None;
    94.                 Cursor.visible = true;
    95.             }
    96.         }
    97.  
    98.         Quaternion ClampRotationAroundXAxis(Quaternion q)
    99.         {
    100.             q.x /= q.w;
    101.             q.y /= q.w;
    102.             q.z /= q.w;
    103.             q.w = 1.0f;
    104.  
    105.             float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
    106.  
    107.             angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
    108.  
    109.             q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
    110.  
    111.             return q;
    112.         }
    113.  
    114.     }
    115. }
    116.  
     
  6. SprinkledSpooks

    SprinkledSpooks

    Joined:
    Jun 1, 2016
    Posts:
    117
    Okay, I just discovered something. In the code I posted above, it states that if the ESCAPE key is pressed, the mouse cursor should become unlocked. So I thought that I would change it to the letter E instead, so that when the user presses E to change cameras, the user will also be unlocking the mouse. However, when I tried this out, I discovered that, once the camera is switched, the mouse is still not visible, and the player must press E again to unlock the mouse. So now I'm thinking it's some sort of transition issue as well.
     
unityunity