Search code examples
c#unity-game-enginecursorvisible

My Cursor Stays Visible When Exiting my Pause Menu Even Though I Have Called the Cursor.Visible Function When Exiting the Menu


I am making a pause menu in my game. My game locks the cursor in position and then hides the cursor so that it is not distracting for the user. This works as intended until I pause the game. The cursor becomes visible (as Cursor.Visible is set to true in the pause function) and the pause menu works correctly. When I press escape again to exit the menu, however, the cursor remains visible in position on the screen.

How can I fix this?

Thanks,

Here is my Code:

using UnityEngine;

public class PlayerCameraRotation : MonoBehaviour
{
    Vector2 mouseDirection;      //Initialises Mouse Direction.                                                                    
    Vector2 smoothValue;         // Initialises The Smoothing Value.                                                                           
    private float sensitivity = 1.0F;     // Sets The Sensitivity To 1.                                                             
    private float smoothing = 2.0F;           // Sets Smoothing To 2.
    private GameObject cameraDirection;       // Initialises Camera Direction.
    Vector2 mouseD;
    private bool gamePause = false;

void Start()                                                                                      // Calls Once As Soon As The Program Is Started.
{
    cameraDirection = transform.parent.gameObject;                                                 // Rotates The Camera To Face The Same Direction As The Player.
}


void LateUpdate()                                                                                 // Updates Every Frame After All Other Updates Have Been Completed.
{
    if(gamePause == false)
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
    mouseD = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    mouseD = Vector2.Scale(mouseD, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
    smoothValue.x = Mathf.Lerp(smoothValue.x, mouseD.x, 1f / smoothing);
    smoothValue.y = Mathf.Lerp(smoothValue.y, mouseD.y, 1f / smoothing);
    mouseDirection += smoothValue;
    mouseDirection.y = Mathf.Clamp(mouseDirection.y, -90f, 90f);

    transform.localRotation = Quaternion.AngleAxis(-mouseDirection.y, Vector3.right);
    cameraDirection.transform.localRotation = Quaternion.AngleAxis(mouseDirection.x, cameraDirection.transform.up);

    if (Input.GetKeyDown("escape"))
    {
        if(gamePause == true)
        {
            ResumeGame();
            gamePause = false;

        }
        else
        {
            PauseGame();
            gamePause = true;
        }
    }
}

private void ResumeGame()
{
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
    smoothing = 2F;
}

private void PauseGame()
{
    Cursor.lockState = CursorLockMode.None;
    Cursor.visible = true;
    smoothing = 0F;
}
}

Solution

  • The problem here seems to be that when you run the code in the Unity Editor, the cursor does not vanish, but when the same code is built and then run, the cursor does vanish as intended. This problem was solved by building the project and running the .exe file.