using UnityEngine; namespace Crosstales.Common.Tool { /// /// A simple free camera to be added to a Unity game object. /// /// Keys: /// wasd / arrows - movement /// q/e - up/down (local space) /// r/f - up/down (world space) /// pageup/pagedown - up/down (world space) /// hold shift - enable fast movement mode /// right mouse - enable free look /// mouse - free look / rotation /// public class FreeCam : MonoBehaviour { #region Variables /// Normal speed of camera movement. public float MovementSpeed = 10f; /// Speed of camera movement when shift is held down. public float FastMovementSpeed = 100f; /// Sensitivity for free look. public float FreeLookSensitivity = 3f; /// Amount to zoom the camera when using the mouse wheel. public float ZoomSensitivity = 10f; /// Amount to zoom the camera when using the mouse wheel (fast mode). public float FastZoomSensitivity = 50f; private Transform tf; private bool looking; #endregion #region MonoBehaviour methods private void Start() { tf = transform; } private void Update() { bool fastMode = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift); float movementSpeed = fastMode ? FastMovementSpeed : MovementSpeed; if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) tf.position += Time.deltaTime * movementSpeed * -tf.right; if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) tf.position += Time.deltaTime * movementSpeed * tf.right; if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) tf.position += Time.deltaTime * movementSpeed * tf.forward; if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) tf.position += Time.deltaTime * movementSpeed * -tf.forward; if (Input.GetKey(KeyCode.Q)) tf.position += Time.deltaTime * movementSpeed * tf.up; if (Input.GetKey(KeyCode.E)) tf.position += Time.deltaTime * movementSpeed * -tf.up; if (Input.GetKey(KeyCode.R) || Input.GetKey(KeyCode.PageUp)) tf.position += Time.deltaTime * movementSpeed * Vector3.up; if (Input.GetKey(KeyCode.F) || Input.GetKey(KeyCode.PageDown)) tf.position += Time.deltaTime * movementSpeed * -Vector3.up; if (looking) { Vector3 localEulerAngles = tf.localEulerAngles; float newRotationX = localEulerAngles.y + Input.GetAxis("Mouse X") * FreeLookSensitivity; float newRotationY = localEulerAngles.x - Input.GetAxis("Mouse Y") * FreeLookSensitivity; localEulerAngles = new Vector3(newRotationY, newRotationX, 0f); tf.localEulerAngles = localEulerAngles; } float axis = Input.GetAxis("Mouse ScrollWheel"); if (Mathf.Abs(axis) > Util.BaseConstants.FLOAT_TOLERANCE) { float zoomSensitivity = fastMode ? FastZoomSensitivity : ZoomSensitivity; tf.position += zoomSensitivity * axis * tf.forward; } if (Input.GetKeyDown(KeyCode.Mouse1)) { StartLooking(); } else if (Input.GetKeyUp(KeyCode.Mouse1)) { StopLooking(); } } private void OnDisable() { StopLooking(); } #endregion #region Public methods /// /// Enable free looking. /// public void StartLooking() { looking = true; Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; } /// /// Disable free looking. /// public void StopLooking() { looking = false; Cursor.visible = true; Cursor.lockState = CursorLockMode.None; } #endregion } } // © 2019-2020 crosstales LLC (https://www.crosstales.com)