I have a cursor lock script set up, and it works pretty good in terms of what its meant to do. But i have one problem, when the cursor is meant to lock, i have to click the screen before it will actually initiate its locked state. I’ve tried a few ways to fix it, and some of them made the script more manageable and direct, but it still never fixed the main issue.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;
using UnityStandardAssets.Characters.FirstPerson;
public class GameManager : MonoBehaviour
{
//----------------------------------------
// handles
public UIManager UI;
public Canvas Pausemenu;
public GameObject PauseOverlay;
public FirstPersonController FP;
public bool CursorL = true;
CursorLockMode wantedMode;
//-----------------------------------------
// function definitions
public void ToggleCursorLock()
{
FP.m_MouseLook.SetCursorLock(CursorL);
Cursor.lockState = wantedMode;
// Hide cursor when locking
Cursor.visible = (CursorLockMode.Locked != wantedMode);
}
public void TogglePauseMenu()
{
// not the optimal way but for the sake of readability
if (Pausemenu.GetComponent<Canvas>().enabled)
{
Pausemenu.GetComponent<Canvas>().enabled = false;
Pausemenu.GetComponent<GraphicRaycaster>().enabled = true;
PauseOverlay.SetActive(false);
Time.timeScale = 1.0f;
CursorL = true;
wantedMode = CursorLockMode.Confined;
}
else
{
Pausemenu.GetComponent<Canvas>().enabled = true;
Time.timeScale = 0f;
CursorL = false;
Cursor.lockState = wantedMode = CursorLockMode.None;
}
Debug.Log("GAMEMANAGER:: TimeScale: " + Time.timeScale);
}
public void MenuToggle()
{
TogglePauseMenu();
ToggleCursorLock();
}
}