Unity3D:实现摄像机围绕目标旋转与缩放的代码方案及优化
# 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()用于确保在所有其他更新完成后执行摄像机移动 ######[AI写代码神器 | 1373点数解答 | 2025-06-25 18:36:24]
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)363
- Unity3D命名空间教程:从基础到最佳实践全解析(DeepSeek | 626点数解答 | 2025-06-25 18:31:57)212
- 工程师必备:十大APP性能优化方案大揭秘(百度文心 | 300点数解答 | 2023-11-09 18:31:04)343
- 含不规则缺陷原材料的最优切割方案建模与损失率、利用率计算(DeepSeek | 2367点数解答 | 2025-05-10 23:47:30)273
- 用 C++ 和 OpenCV 生成带随机偏移的圆周上的点:代码剖析(字节豆包 | 29点数解答 | 2024-11-06 23:40:42)269
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)334
- 解析React 15与Vue 2:从O(n^3)到O(n),Diff算法时间复杂度计算及性能对比 (阿里通义 | 379点数解答 | 2023-11-09 01:49:19)346
- 围绕四大“带头”查摆问题:个人对照检查及整改提升之路 (字节豆包 | 903点数解答 | 2025-03-03 09:50:25)167
- 缜密逻辑,全新无名杀“风餐”技能代码精心打造与深度解析 (字节豆包 | 469点数解答 | 2025-04-05 11:37:06)258
- 精准优化:明确需求,开启高效提升之旅! (字节豆包 | 52点数解答 | 2026-02-09 16:44:46)53
- 代码再升级:优化版“获取dump”代码,多机制保障数据读取与保存(字节豆包 | 734点数解答 | 2026-02-10 01:23:31)77
- 代码深度优化:全面检查异常、灵活读取内存,提升可读性与可维护性(字节豆包 | 959点数解答 | 2026-02-10 01:24:14)46