using UnityEngine; namespace FIMSpace.Basics { /// /// FM: Simple component to controll behaviour of camera in free flying mode /// public class FBasic_FreeCameraBehaviour : MonoBehaviour { [Header("> Hold right mouse button to rotate camera <")] [Tooltip("How fast camera should fly")] public float SpeedMultiplier = 10f; [Tooltip("Value of acceleration smoothness")] public float AccelerationSmothnessValue = 10f; [Tooltip("Value of rotation smoothness")] public float RotationSmothnessValue = 10f; /// Just multiplier for rotation public float MouseSensitivity = 5f; public bool NeedRMB = true; /// Variables controlling turbo speed on shift key private float turboModeMultiply = 5f; /// Variable to hold speeds of directions for camera to fly private Vector3 speeds; private float ySpeed; /// Holding rotation value for camera to rotate private Vector3 rotation; /// Turbo multiplier for faster movement private float turbo = 1f; /// /// Just initializing few variables /// void Start() { speeds = Vector3.zero; ySpeed = 0f; rotation = transform.rotation.eulerAngles; } void Update() { // Detecting key movement factors float f = Input.GetAxis("Vertical"); float r = Input.GetAxis("Horizontal"); float forward = f * Time.smoothDeltaTime * SpeedMultiplier; float right = r * Time.smoothDeltaTime * SpeedMultiplier; // Smooth change turbo speed if (Input.GetKey(KeyCode.LeftShift)) { turbo = Mathf.Lerp(turbo, turboModeMultiply, Time.smoothDeltaTime * 5f); } else { turbo = Mathf.Lerp(turbo, 1f, Time.smoothDeltaTime * 5f); } forward *= turbo; right *= turbo; if (Cursor.lockState != CursorLockMode.None) { // Rotation with pressed rmb if (Input.GetMouseButton(1) || !NeedRMB ) { rotation.x -= (Input.GetAxis("Mouse Y") * 1f * MouseSensitivity); rotation.y += (Input.GetAxis("Mouse X") * 1f * MouseSensitivity); } } // Lerping speed variables for smooth effect speeds.z = Mathf.Lerp(speeds.z, forward, Time.smoothDeltaTime * AccelerationSmothnessValue); speeds.x = Mathf.Lerp(speeds.x, right, Time.smoothDeltaTime * AccelerationSmothnessValue); // Applying translation for current transform orientation transform.position += transform.forward * speeds.z; transform.position += transform.right * speeds.x; transform.position += transform.up * speeds.y; // Lerping rotation for smooth effect transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(rotation), Time.smoothDeltaTime * RotationSmothnessValue); // Going in Up / Down directions in world reference if (Input.GetKey(KeyCode.LeftControl)) { ySpeed = Mathf.Lerp(ySpeed, 1f, Time.smoothDeltaTime * AccelerationSmothnessValue); } else if (Input.GetButton("Jump")) { ySpeed = Mathf.Lerp(ySpeed, -1f, Time.smoothDeltaTime * AccelerationSmothnessValue); } else ySpeed = Mathf.Lerp(ySpeed, 0f, Time.smoothDeltaTime * AccelerationSmothnessValue); transform.position += Vector3.down * ySpeed * turbo * Time.smoothDeltaTime * SpeedMultiplier; } /// /// Cursor locking stuff /// public void FixedUpdate() { if (Input.GetMouseButton(1)) { Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } else { if (NeedRMB) { Cursor.lockState = CursorLockMode.None; Cursor.visible = true; } } } } }