# Unity3D摄像机以目标为中心旋转与缩放实现 要实现摄像机围绕目标物体旋转和缩放的功能,可以使用以下代码方案: ## 基本实现方案 ```csharp using UnityEngine; public class CameraOrbit : MonoBehaviour { public Transform target; // 要围绕的目标物体 public float distance = 5.0f; // 初始距离 public float zoomSpeed = 2.0f; // 缩放速度 public float xSpeed = 120.0f; // X轴旋转速度 public float ySpeed = 120.0f; // Y轴旋转速度 public float yMinLimit = -20f; // Y轴旋转最小角度 public float yMaxLimit = 80f; // Y轴旋转最大角度 public float distanceMin = 0.5f; // 最小缩放距离 public float distanceMax = 15f; // 最大缩放距离 private float x = 0.0f; // X轴旋转角度 private float y = 0.0f; // Y轴旋转角度 void Start() { Vector3 angles = transform.eulerAngles; x = angles.y; y = angles.x; } void LateUpdate() { if (target) { // 鼠标右键旋转 if (Input.GetMouseButton(1)) { x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; y = ClampAngle(y, yMinLimit, yMaxLimit); } // 鼠标滚轮缩放 distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * zoomSpeed, distanceMin, distanceMax); // 计算旋转和位置 Quaternion rotation = Quaternion.Euler(y, x, 0); Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance); Vector3 position = rotation * negDistance + target.position; // 应用变换 transform.rotation = rotation; transform.position = position; } } // 限制角度范围 public static float ClampAngle(float angle, float min, float max) { if (angle < -360F) angle += 360F; if (angle > 360F) angle -= 360F; return Mathf.Clamp(angle, min, max); } } ``` ## 优化版本(带平滑移动) ```csharp using UnityEngine; public class SmoothCameraOrbit : MonoBehaviour { public Transform target; public float distance = 5.0f; public float zoomSpeed = 2.0f; public float xSpeed = 120.0f; public float ySpeed = 120.0f; public float yMinLimit = -20f; public float yMaxLimit = 80f; public float distanceMin = 0.5f; public float distanceMax = 15f; public float smoothTime = 0.3f; // 平滑移动时间 private float rotationYAxis = 0.0f; private float rotationXAxis = 0.0f; private float velocityX = 0.0f; private float velocityY = 0.0f; private float currentDistance; private float desiredDistance; private Vector3 position; private Quaternion rotation; void Start() { Vector3 angles = transform.eulerAngles; rotationYAxis = angles.y; rotationXAxis = angles.x; currentDistance = distance; desiredDistance = distance; } void LateUpdate() { if (target) { // 鼠标右键旋转 if (Input.GetMouseButton(1)) { velocityX += xSpeed * Input.GetAxis("Mouse X") * 0.02f; velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f; } // 鼠标滚轮缩放 desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed; desiredDistance = Mathf.Clamp(desiredDistance, distanceMin, distanceMax); currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomSpeed); // 计算旋转角度 rotationYAxis += velocityX; rotationXAxis -= velocityY; rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit); // 平滑阻尼 velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime); velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime); // 计算旋转和位置 rotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0); position = rotation * new Vector3(0.0f, 0.0f, -currentDistance) + target.position; // 应用变换 transform.rotation = rotation; transform.position = position; } } public static float ClampAngle(float angle, float min, float max) { if (angle < -360F) angle += 360F; if (angle > 360F) angle -= 360F; return Mathf.Clamp(angle, min, max); } } ``` ## 使用说明 1. 将脚本附加到摄像机对象上 2. 在Inspector面板中设置目标物体(target) 3. 调整参数如旋转速度、缩放范围等 4. 运行场景后: - 按住鼠标右键并移动可以旋转摄像机 - 使用鼠标滚轮可以缩放摄像机 ## 注意事项 1. 确保目标物体(target)已正确设置,否则摄像机不会跟随任何物体 2. 旋转角度限制(yMinLimit/yMaxLimit)可以防止摄像机翻转 3. 平滑版本更适合实际游戏应用,避免摄像机移动过于生硬 4. LateUpdate()用于确保在所有其他更新完成后执行摄像机移动 [2025-06-25 18:36:24 | AI写代码神器 | 1373点数解答]