daily pastebin goal
34%
SHARE
TWEET

CursorLock.cs

mvaganov Feb 26th, 2018 (edited) 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // author: mvaganov@hotmail.com
  6. // license: Copyfree, public domain. This is free code! Great artists, steal this code!
  7. // latest version at: https://pastebin.com/raw/zqd0yK40
  8. namespace NS {
  9.     public class CursorLock : MonoBehaviour {
  10.         void FixedUpdate() {
  11.             LockUpdate();
  12.         }
  13.         // whether cursor is visible or not
  14.         private static bool m_cursorIsLocked = false;
  15.         public static void LockUpdate() {
  16.             if(Input.GetKeyUp(KeyCode.Escape)) {
  17.                 m_cursorIsLocked = false;
  18.             } else if(Input.GetMouseButtonUp(0)) {
  19.                 m_cursorIsLocked = true;
  20.             }
  21.  
  22.             if (m_cursorIsLocked) {
  23.                 Cursor.lockState = CursorLockMode.Locked;
  24.                 Cursor.visible = false;
  25.             } else if (!m_cursorIsLocked) {
  26.                 Cursor.lockState = CursorLockMode.None;
  27.                 Cursor.visible = true;
  28.             }
  29.         }
  30.     }
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top