Skip to content

Instantly share code, notes, and snippets.

@wcoastsands
Created July 13, 2016 06:52
Show Gist options
  • Save wcoastsands/b10b2a5658cfa0a67092211ac4d3b02d to your computer and use it in GitHub Desktop.
Save wcoastsands/b10b2a5658cfa0a67092211ac4d3b02d to your computer and use it in GitHub Desktop.
CursorManager for Unity Standard Assets FirstPersonController
using UnityEngine;
using FirstPersonController = UnityStandardAssets.Characters.FirstPerson.FirstPersonController;
public class CursorManager : MonoBehaviour
{
public FirstPersonController fpsController;
public bool locked { get; private set; }
void OnEnable ()
{
if (!fpsController)
{
fpsController = FindObjectOfType<FirstPersonController>();
if (!fpsController)
{
enabled = false;
Debug.LogWarning("FirstPersonController object not found. CursorManager will remain disabled.");
}
}
if (fpsController) SetCursorLockedAndHidden(true);
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
fpsController.enabled = false;
SetCursorLockedAndHidden(false);
}
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
{
fpsController.enabled = true;
SetCursorLockedAndHidden(true);
}
}
void SetCursorLockedAndHidden (bool locked)
{
Cursor.lockState = (locked ? CursorLockMode.Locked : CursorLockMode.None);
Cursor.visible = !locked;
this.locked = locked;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment