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;
}
}
Answer by Adventuruss · Jun 22, 2017 at 10:58 AM
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;
@Adventuruss the cursor is visible but we can not use it in the game to click buttons how can i solve that i used your above code thank you
Answer by RaDus1 · Aug 12, 2016 at 04:04 PM
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?