How to lock / unlock Cursor and Mouselook when using FPSController

I’m working on a Unity 5 project and am using the First Person Character prefab to handle the player character. I’m struggling to implement a pause menu because I cannot figure out how to correctly lock and unlock the cursor and mouselook.

In my current code, the cursor unlocks when the game is paused, but locks as soon as the player clicks (even if they game is still paused). It also fails to lock when the game is unpaused, again unless the player clicks. The MouseLook also never seems to disable, meaning the camera is visibly swinging around when the game is paused.

I want a way to lock the mouse look and unlock the cursor for the entire time the game is paused so that the player can interact with the menu, and do the reverse whenever the game is not paused to let the player play the game.

Here is a sample of my UI controller that is meant to handle the logic for pausing and unpausing the game:

public class UIManager : MonoBehaviour {
	private AudioSource pauseSource;

	public GameObject PanelPause;
    public SettingsManager settingsManager;
	public FirstPersonController player;
	public bool isPaused = false;

    void Start () {
        pauseSource = GetComponent<AudioSource> ();
        
        pauseSource.ignoreListenerPause = true;
	    pauseSource.Play (); 
	    pauseSource.Pause ();
        
        PanelPause.SetActive(isPaused);
    }
        
    void Update () {
	    if (Input.GetButtonDown("Cancel")) {
            isPaused = !isPaused;
	    }

        if (!isPaused) {
            PanelControls.SetActive(viewControls);
        }

        setPause(isPaused);
	}

        public void setPause(bool pauseState) {
            player.m_MouseLook.SetCursorLock(!pauseState);

            PanelPause.SetActive(pauseState);
            AudioListener.pause = pauseState;

            if (pauseState) {
                // Do logic to pause
                Time.timeScale = 0.0f;

                pauseSource.Pause();

                Cursor.lockState = CursorLockMode.None;
            } else {
                // Do logic to unpause
                Time.timeScale = 1.0f;

                pauseSource.UnPause();

                settingsManager.exitSettings();

                Cursor.lockState = CursorLockMode.Locked;
            }
	}

In order to unlock the cursor with the First Person Controller, you need to know where the FPC script is located in relation to your code. For me, when I imported it into the scene, I left it in its default folder. Because of that, I had to access it through that folder structure. You also need to make the m_MouseLook variable public in the FirstPersonController script, and the m_cursorIsLocked variable in the MouseLook script in order to access them.This will still allow the mouse movement to control the camera, but will also free the mouse.

public GameObject FPC;  // Assign the First Person Controller to this in the Editor.

FPC.GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().m_MouseLook.m_cursorIsLocked = false;

I don’t know the context, but wouldn’t it be better to ignore the paused state and just unlock/lock the mouse movement when called for?

using UnityEngine.SceneManagement;
using UnityEngine;

public class PausedMenu : MonoBehaviour
{
public GameObject Pause_Menu;
public AtesEtme ateset;
public Pistol pistol;
public UnityStandardAssets.Characters.FirstPerson.FirstPersonController player;
bool isPaused;

private Vector3 lastMousePosition;

private void Awake()
{
    Cursor.lockState = CursorLockMode.None;
    Time.timeScale = 1;
    Cursor.visible = true;
    pistol = GetComponentInChildren<Pistol>();
}

void Update()
{
    if (Input.GetKeyDown(KeyCode.Escape))
    {
        isPaused = !isPaused;
    }

    if (isPaused)
    {
        Pause_Menu.SetActive(true);
        Time.timeScale = 0;
        pistol.enabled = false;
        player.enabled = false;
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
    }
    else
    {
        Pause_Menu.SetActive(false);
        Time.timeScale = 1;
        player.enabled = true;
        pistol.enabled = true;
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    
}

public void ResumeGame()
{
    isPaused = false;
}

public void ExitGame()
{
    Application.Quit();
}

public void MainMenu()
{
    Time.timeScale = 1;
    SceneManager.LoadScene(1);
}

}