using System.IO; using UnityEngine; public class FlyCamera : MonoBehaviour { [Header("Movement Settings")] public float moveSpeed = 10f; public float fastMoveSpeed = 50f; public float mouseSensitivity = 2f; [Header("FOV Settings")] public float minFOV = 10f; public float maxFOV = 120f; public float fovScrollSpeed = 10f; [Header("Screenshot Settings")] public KeyCode screenshotKey = KeyCode.F2; private Camera cam; private float rotationX = 0f; private bool isFlyCamEnabled = true; private float screenshotFlashTime = 0f; private bool showScreenshotFlash = false; private void Start() { cam = GetComponent(); if (cam == null) { cam = Camera.main; } Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; CreateScreenshotDirectory(); } private void Update() { if (!isFlyCamEnabled) return; HandleMouseLook(); HandleMovement(); HandleFOVControl(); HandleScreenshot(); HandleScreenshotFlash(); if (Input.GetKeyDown(KeyCode.Escape)) { ToggleCursor(); } if (Cursor.lockState != CursorLockMode.Locked && Input.GetMouseButtonDown(0)) { Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } } private void HandleMouseLook() { if (Cursor.lockState != CursorLockMode.Locked) return; float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity; float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity; rotationX -= mouseY; rotationX = Mathf.Clamp(rotationX, -90f, 90f); transform.rotation = Quaternion.Euler(rotationX, transform.eulerAngles.y + mouseX, 0f); } private void HandleMovement() { Vector3 movement = Vector3.zero; float currentSpeed = Input.GetKey(KeyCode.LeftShift) ? fastMoveSpeed : moveSpeed; if (Input.GetKey(KeyCode.W)) movement += transform.forward; if (Input.GetKey(KeyCode.S)) movement -= transform.forward; if (Input.GetKey(KeyCode.A)) movement -= transform.right; if (Input.GetKey(KeyCode.D)) movement += transform.right; if (Input.GetKey(KeyCode.Q)) movement -= transform.up; if (Input.GetKey(KeyCode.E)) movement += transform.up; transform.position += movement * currentSpeed * Time.deltaTime; } private void HandleFOVControl() { float scroll = Input.GetAxis("Mouse ScrollWheel"); if (scroll != 0f) { cam.fieldOfView = Mathf.Clamp(cam.fieldOfView - scroll * fovScrollSpeed, minFOV, maxFOV); } } private void HandleScreenshot() { if (Input.GetKeyDown(screenshotKey)) { TakeScreenshot(); } } private void CreateScreenshotDirectory() { string screenshotDir = Path.Combine(Application.dataPath, "..", "Screenshots"); if (!Directory.Exists(screenshotDir)) { Directory.CreateDirectory(screenshotDir); } } private void TakeScreenshot() { StartCoroutine(CaptureScreenshotDelayed()); } private System.Collections.IEnumerator CaptureScreenshotDelayed() { yield return new WaitForEndOfFrame(); string timestamp = System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); string filename = $"Screenshot_{timestamp}.png"; string screenshotDir = Path.Combine(Application.dataPath, "..", "Screenshots"); string fullPath = Path.Combine(screenshotDir, filename); ScreenCapture.CaptureScreenshot(fullPath); Debug.Log($"Screenshot saved: {fullPath}"); showScreenshotFlash = true; screenshotFlashTime = 0.2f; } private void HandleScreenshotFlash() { if (showScreenshotFlash) { screenshotFlashTime -= Time.deltaTime; if (screenshotFlashTime <= 0f) { showScreenshotFlash = false; } } } private void OnGUI() { if (showScreenshotFlash) { GUI.color = new Color(1f, 1f, 1f, 0.8f); GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Texture2D.whiteTexture); GUIStyle style = new GUIStyle(); style.fontSize = 36; style.normal.textColor = Color.black; style.alignment = TextAnchor.MiddleCenter; GUI.Label(new Rect(0, Screen.height / 2 - 50, Screen.width, 100), "Screenshot Captured!", style); } } private void ToggleCursor() { if (Cursor.lockState == CursorLockMode.Locked) { Cursor.lockState = CursorLockMode.None; Cursor.visible = true; } else { Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } } public void EnableFlyCamera(bool enable) { isFlyCamEnabled = enable; if (enable) { Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } else { Cursor.lockState = CursorLockMode.None; Cursor.visible = true; } } }