经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » unity » 查看文章
unity 实现摄像机绕某点旋转一周
来源:jb51  时间:2021/4/12 19:00:21  对本文有异议

在Update函数中执行:

  1. if (turnAround)
  2. {
  3. playerCamera.RotateAround(transform.localPosition, transform.up, Time.deltaTime * rotateSpeed);
  4. degree += Time.deltaTime * rotateSpeed;
  5. if (degree >= 360)
  6. {
  7. turnAround = false;
  8. degree = 0;
  9. }
  10. }

补充:unity 摄像机围绕某个物体进行旋转放大缩小

脚本通过以一个物体为中心点

来控制摄像机围绕物体旋转缩放 ,脚本挂在摄像机上即可,target是需要观看的物体

  1. public Transform Camera2;
  2. public GameObject target;
  3. private float dis;
  4. public float xSpeed = 200, ySpeed = 200, mSpeed = 10; //移动速度
  5. public float yMinLimit = -50, yMaxLimit = 50; //摄像机的Y轴移动最小最大限制
  6. public float distance = 7, minDistance = 2, maxDistance = 30; //摄像机与目标物体的距离
  7. public bool needDamping = true; //阻尼默认开启
  8. float damping = 5.0f; //默认阻尼为5.0F
  9. public float x = 0.0f; //X轴
  10. public float y = 0.0f; //Y轴
  11. // Use this for initialization
  12. void Start() {
  13. instance = this;
  14. Camr = Camera2Rotation.Close;
  15. Vector3 angles = transform.eulerAngles;
  16. x = angles.y;
  17. y = angles.x;
  18. }
  19. private void Update()
  20. {
  21. }
  22. void LateUpdate()
  23. {
  24. //使用按下鼠标左键移动物体
  25. if (Input.GetMouseButton(1))
  26. {
  27. x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
  28. y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
  29. y = ClampAngle(y, yMinLimit, yMaxLimit);
  30. }
  31. distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;
  32. distance = Mathf.Clamp(distance, minDistance, maxDistance);
  33. Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
  34. Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
  35. Vector3 position = rotation * disVector + target.transform.position;
  36. //adjust the camera
  37. if (needDamping)
  38. {
  39. transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
  40. transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
  41. }
  42. else
  43. {
  44. transform.rotation = rotation;
  45. transform.position = position;
  46. }
  47. }
  48. /// <summary>
  49. /// 旋转角度的控制
  50. /// </summary>
  51. /// <param name="angle">旋转的角度</param>
  52. /// <param name="min">最小角度</param>
  53. /// <param name="max">最大角度</param>
  54. /// <returns></returns>
  55. static float ClampAngle(float angle, float min, float max)
  56. {
  57. if (angle < -360)
  58. angle += 360;
  59. if (angle > 360)
  60. angle -= 360;
  61. return Mathf.Clamp(angle, min, max);
  62. }
  63. void MoveCameraToTarget()
  64. {
  65. if (target != null)
  66. {
  67. Camera2.LookAt(target.transform);
  68. Camera2.position = Vector3.MoveTowards(Camera2.position, target.transform.position, 5 * Time.deltaTime);
  69. dis = Vector3.Distance(Camera2.transform.position, target.transform.position);
  70. if (dis < 1.5f)
  71. {
  72. Camr = Camera2Rotation.Open;
  73. CancelInvoke();
  74. }
  75. }
  76. }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。如有错误或未考虑完全的地方,望不吝赐教。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号