using UnityEngine; public class FlyCamera : MonoBehaviour { public float movementSpeed = 10.0f; // 基础移动速度 public float fastSpeed = 50.0f; // 加速移动速度(按住 Shift) public float mouseSensitivity = 3.0f; // 鼠标灵敏度 private float yaw = 0.0f; // 水平旋转角度 private float pitch = 0.0f; // 垂直旋转角度 void Start() { // 锁定并隐藏鼠标光标 Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } void Update() { // 鼠标控制视角 float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity; float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity; yaw += mouseX; pitch -= mouseY; pitch = Mathf.Clamp(pitch, -90f, 90f); // 限制垂直旋转角度 transform.eulerAngles = new Vector3(pitch, yaw, 0.0f); // 键盘控制移动 float currentSpeed = Input.GetKey(KeyCode.LeftShift) ? fastSpeed : movementSpeed; Vector3 direction = new Vector3(); if (Input.GetKey(KeyCode.W)) direction += transform.forward; if (Input.GetKey(KeyCode.S)) direction -= transform.forward; if (Input.GetKey(KeyCode.A)) direction -= transform.right; if (Input.GetKey(KeyCode.D)) direction += transform.right; if (Input.GetKey(KeyCode.Space)) direction += transform.up; if (Input.GetKey(KeyCode.LeftControl)) direction -= transform.up; transform.position += direction.normalized * currentSpeed * Time.deltaTime; } }