Created
February 10, 2017 17:36
-
-
Save wcoastsands/dff2b423120b539bdd412d74ec56b04f to your computer and use it in GitHub Desktop.
CursorController for MouseLook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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