CameraController.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. /// <summary>
  6. /// 相机漫游控制
  7. /// </summary>
  8. public class CameraController : MonoBehaviour
  9. {
  10. public static CameraController instance;
  11. [Header("是否可移动")]
  12. public bool canMove = true;
  13. [Header("是否可转动")]
  14. public bool camRotate = true;
  15. public bool _isCouldCrossCollider = false;//是否可穿碰撞器
  16. public float _moveSpeed = 10f;//移动速度
  17. public float _xSpeed = 250.0f;//X轴视角旋转速度
  18. public float _ySpeed = 120.0f;//Y轴视角旋转速度
  19. public float _mouseSpeed = 500f;//鼠标中间滚轮的拉近拉远速度
  20. public float _yMinLimit = -20;//视角旋转向下最小限制
  21. public float _yMaxLimit = 80;//视角旋转向上最大限制
  22. public float _speedLimit = 0.2f;//鼠标中键移动速度限制
  23. private float _angleX = 0.0f;
  24. private float _angleY = 0.0f;
  25. private float _moveV = 0.0f;
  26. private float _moveH = 0.0f;
  27. private Vector3 _movePos;
  28. private float xMax;
  29. private float xMin;
  30. private float yMax;
  31. private float yMin;
  32. private float zMax;
  33. private float zMin;
  34. private Vector3 initPostion;
  35. private Quaternion initRotation;
  36. /// <summary>
  37. /// 初始化摄像机,避免产生空引用
  38. /// </summary>
  39. void Start()
  40. {
  41. initPostion = transform.position;
  42. initRotation = transform.rotation;
  43. instance = this;
  44. ResetInfo();
  45. }
  46. public void SetCamerPositionAndRotation(Vector3 positon, Vector3 rotation)
  47. {
  48. _movePos = positon;
  49. this.transform.position = positon;
  50. transform.eulerAngles = rotation;
  51. }
  52. public void SetInitPositionAndRotation()
  53. {
  54. transform.position = initPostion;
  55. transform.rotation = initRotation;
  56. }
  57. /// <summary>
  58. /// 重置位置信息
  59. /// </summary>
  60. public void ResetInfo()
  61. {
  62. var angles = transform.eulerAngles;
  63. _angleX = angles.y;
  64. _angleY = angles.x;
  65. _movePos = this.transform.position;
  66. }
  67. //Update is called once per frame
  68. void Update()
  69. {
  70. if (EventSystem.current.IsPointerOverGameObject()) return;
  71. if (canMove)
  72. {
  73. moveCamera();
  74. riseCamera();
  75. }
  76. if (camRotate) rotateView();
  77. viewScale();
  78. SetMovePosToThis();
  79. }
  80. /// <summary>
  81. /// 前后移动摄像机
  82. /// </summary>
  83. void moveCamera()
  84. {
  85. _moveV = Input.GetAxis("Vertical") * Time.deltaTime * _moveSpeed;
  86. _moveH = Input.GetAxis("Horizontal") * Time.deltaTime * _moveSpeed;
  87. _movePos = this.transform.position + this.transform.forward * _moveV + this.transform.right * _moveH;
  88. }
  89. /// <summary>
  90. /// 自身位置赋值
  91. /// </summary>
  92. private void SetMovePosToThis()
  93. {
  94. if (_isCouldCrossCollider)
  95. {
  96. this.transform.position = _movePos;
  97. }
  98. else
  99. {
  100. if (CameraRestraint.CheckThePos(this.transform, _movePos))
  101. {
  102. this.transform.position = _movePos;
  103. }
  104. }
  105. }
  106. public void SetCameraPosLimite(float _xMin, float _xMax, float _yMin, float _yMax, float _zMin, float _zMax)
  107. {
  108. xMax = _xMax; xMin = _xMin;
  109. yMax = _yMax; yMin = _yMin;
  110. zMax = _zMax; zMin = _zMin;
  111. }
  112. /// <summary>
  113. /// 鼠标中键控制相机移动
  114. /// </summary>
  115. void riseCamera()
  116. {
  117. if (Input.GetMouseButton(2))
  118. {
  119. _moveV = -Input.GetAxis("Mouse Y") * _moveSpeed;
  120. _moveH = -Input.GetAxis("Mouse X") * _moveSpeed;
  121. _movePos = this.transform.position + this.transform.up * _moveV * Time.deltaTime + this.transform.right * _moveH * Time.deltaTime;
  122. }
  123. }
  124. /// <summary>
  125. /// 鼠标右键控制视角旋转
  126. /// </summary>
  127. void rotateView()
  128. {
  129. if (Input.GetMouseButton(1))
  130. {
  131. _angleX += Input.GetAxis("Mouse X") * _xSpeed * 0.02f;
  132. _angleY -= Input.GetAxis("Mouse Y") * _ySpeed * 0.02f;
  133. _angleY = ClampAngle(_angleY, _yMinLimit, _yMaxLimit);
  134. var rotation = Quaternion.Euler(_angleY, _angleX, 0);
  135. transform.rotation = rotation;
  136. }
  137. }
  138. /// <summary>
  139. /// 滑轮控制视角移动
  140. /// </summary>
  141. void viewScale()
  142. {
  143. if (Input.GetAxis("Mouse ScrollWheel") != 0)
  144. {
  145. _movePos = this.transform.position + this.transform.forward * Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * _mouseSpeed;
  146. }
  147. }
  148. /// <summary>
  149. /// 控制摄像机的旋转角度大小
  150. /// </summary>
  151. /// <param 角度值="angle"></param>
  152. /// <param 最小角度="min"></param>
  153. /// <param 最大角度="max"></param>
  154. /// <returns></returns>
  155. static float ClampAngle(float angle, float min, float max)
  156. {
  157. if (angle < -360)
  158. {
  159. angle += 360;
  160. }
  161. if (angle > 360)
  162. {
  163. angle -= 360;
  164. }
  165. return Mathf.Clamp(angle, min, max);
  166. }
  167. }