Skip to content

Instantly share code, notes, and snippets.

@wcoastsands
Created February 10, 2017 17:36
Show Gist options
  • Save wcoastsands/dff2b423120b539bdd412d74ec56b04f to your computer and use it in GitHub Desktop.
Save wcoastsands/dff2b423120b539bdd412d74ec56b04f to your computer and use it in GitHub Desktop.
CursorController for MouseLook
using UnityEngine;
[RequireComponent(typeof(MouseLook))]
public class CursorController : MonoBehaviour
{
MouseLook m_MouseLook;
public bool locked { get; private set; }
void OnEnable ()
{
if (!m_MouseLook)
{
m_MouseLook = GetComponent<MouseLook>();
if (!m_MouseLook)
{
enabled = false;
Debug.LogWarning("FirstPersonController object not found. CursorManager will remain disabled.");
}
}
if (m_MouseLook)
SetCursorLockedAndHidden(true);
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
m_MouseLook.enabled = false;
SetCursorLockedAndHidden(false);
}
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
{
m_MouseLook.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