当前位置:   article > 正文

Unity3D 相机绕对象旋转和调整距离_unity 围绕物体旋转距离保持不变

unity 围绕物体旋转距离保持不变
  1. using UnityEngine;
  2. public class MouseFollowRotation : MonoBehaviour
  3. {
  4. public Transform target;
  5. public float xSpeed = 200;
  6. public float ySpeed = 200;
  7. public float mSpeed = 10;
  8. public float yMinLimit = -50;
  9. public float yMaxLimit = 50;
  10. public float distance = 2;
  11. public float minDistance = 2;
  12. public float maxDistance = 30;
  13. //bool needDamping = false;
  14. public bool needDamping = true;
  15. float damping = 5.0f;
  16. public float x = 0.0f;
  17. public float y = 0.0f;
  18. // Use this for initialization
  19. void Start()
  20. {
  21. Vector3 angles = transform.eulerAngles;
  22. x = angles.y;
  23. y = angles.x;
  24. }
  25. // Update is called once per frame
  26. void LateUpdate()
  27. {
  28. if (target)
  29. {
  30. //use the light button of mouse to rotate the camera
  31. if (Input.GetMouseButton(1))
  32. {
  33. x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
  34. y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
  35. y = ClampAngle(y, yMinLimit, yMaxLimit);
  36. }
  37. distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;
  38. distance = Mathf.Clamp(distance, minDistance, maxDistance);
  39. Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
  40. Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
  41. Vector3 position = rotation * disVector + target.position;
  42. //adjust the camera
  43. if (needDamping)
  44. {
  45. transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
  46. transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
  47. }
  48. else
  49. {
  50. transform.rotation = rotation;
  51. transform.position = position;
  52. }
  53. }
  54. }
  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. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/40731?site
推荐阅读
相关标签
  

闽ICP备14008679号