FreeCameraController.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. public class FreeCameraController : MonoSingleton<FreeCameraController>
  6. {
  7. // 模型
  8. public Transform model;
  9. // 旋转速度
  10. public float rotateSpeed = 32f;
  11. public float rotateLerp = 8;
  12. // 移动速度
  13. public float moveSpeed = 1f;
  14. public float moveLerp = 10f;
  15. // 镜头拉伸速度
  16. public float zoomSpeed = 10f;
  17. public float zoomLerp = 4f;
  18. // 计算移动
  19. private Vector3 position, targetPosition;
  20. // 计算旋转
  21. private Quaternion rotation, targetRotation;
  22. // 计算距离
  23. private float distance, targetDistance;
  24. // 默认距离
  25. private const float default_distance = 40f;
  26. // y轴旋转范围
  27. private const float min_angle_y = -89f;
  28. private const float max_angle_y = 89f;
  29. public float maxDistance = 30;
  30. private FreeCameraData currentCameraData;
  31. private FreeCameraData allModelData;
  32. public float GetCurrentDistance
  33. {
  34. get { return distance; }
  35. }
  36. // Use this for initialization
  37. void Start()
  38. {
  39. Init();
  40. /*
  41. // 旋转归零
  42. //targetRotation = Quaternion.Euler(30,200,0);
  43. targetRotation = Quaternion.Euler(10, 180, 0);
  44. // 初始位置是模型
  45. targetPosition = model.position;
  46. // 初始镜头拉伸
  47. targetDistance = 85;*/
  48. }
  49. private void Init()
  50. {
  51. allModelData = this.GetComponent<FreeCameraData>();
  52. if (allModelData == null)
  53. {
  54. allModelData = new FreeCameraData() { targetRot = new Vector3(10, 180, 0), model = this.model, targetDistance = 85 };
  55. }
  56. InitCameraPos(allModelData);
  57. }
  58. private void OnEnable()
  59. {
  60. Init();
  61. }
  62. private void OnDisable()
  63. {
  64. Init();
  65. }
  66. // Update is called once per frame
  67. void Update()
  68. {
  69. //if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2))
  70. //{
  71. // if (IsPointerOverGameObject(Input.mousePosition))
  72. // {
  73. // //Debug.Log("点击到UI");
  74. // return;
  75. // }
  76. //}
  77. float dx = Input.GetAxis("Mouse X");
  78. float dy = Input.GetAxis("Mouse Y");
  79. // 异常波动
  80. if (Mathf.Abs(dx) > 5f || Mathf.Abs(dy) > 5f)
  81. {
  82. return;
  83. }
  84. float d_target_distance = targetDistance;
  85. if (d_target_distance < 2f)
  86. {
  87. d_target_distance = 2f;
  88. }
  89. // 鼠标右键移动
  90. if (Input.GetMouseButton(1))
  91. {
  92. dx *= moveSpeed * d_target_distance / default_distance;
  93. dy *= moveSpeed * d_target_distance / default_distance;
  94. targetPosition -= transform.up * dy + transform.right * dx;
  95. }
  96. // 鼠标左键旋转
  97. if (Input.GetMouseButton(0) || Input.GetMouseButton(2))
  98. {
  99. dx *= rotateSpeed;
  100. dy *= rotateSpeed;
  101. if (Mathf.Abs(dx) > 0 || Mathf.Abs(dy) > 0)
  102. {
  103. // 获取摄像机欧拉角
  104. Vector3 angles = transform.rotation.eulerAngles;
  105. // 欧拉角表示按照坐标顺序旋转,比如angles.x=30,表示按x轴旋转30°,dy改变引起x轴的变化
  106. angles.x = Mathf.Repeat(angles.x + 180f, 360f) - 180f;
  107. angles.y += dx;
  108. angles.x -= dy;
  109. angles.x = ClampAngle(angles.x, min_angle_y, max_angle_y);
  110. // 计算摄像头旋转
  111. targetRotation.eulerAngles = new Vector3(angles.x, angles.y, 0);
  112. // 随着旋转,摄像头位置自动恢复
  113. // Vector3 temp_position =
  114. // Vector3.Lerp(targetPosition, model.position, Time.deltaTime * moveLerp);
  115. // targetPosition = Vector3.Lerp(targetPosition, temp_position, Time.deltaTime * moveLerp);
  116. }
  117. }
  118. if (Input.GetKey(KeyCode.R))
  119. {
  120. // 获取摄像机欧拉角
  121. Vector3 angles = transform.rotation.eulerAngles;
  122. // 欧拉角表示按照坐标顺序旋转,比如angles.x=30,表示按x轴旋转30°,dy改变引起x轴的变化
  123. angles.x = Mathf.Repeat(angles.x + 180f, 360f) - 180f;
  124. angles.y -= 3;
  125. angles.x = ClampAngle(angles.x, min_angle_y, max_angle_y);
  126. // 计算摄像头旋转
  127. targetRotation.eulerAngles = new Vector3(angles.x, angles.y, 0);
  128. }
  129. //// 上移
  130. //if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
  131. //{
  132. // targetPosition -= transform.up * d_target_distance / (2f * default_distance);
  133. //}
  134. //// 下移
  135. //if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
  136. //{
  137. // targetPosition += transform.up * d_target_distance / (2f * default_distance);
  138. //}
  139. //// 左移
  140. //if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
  141. //{
  142. // targetPosition += transform.right * d_target_distance / (2f * default_distance);
  143. //}
  144. //// 右移
  145. //if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
  146. //{
  147. // targetPosition -= transform.right * d_target_distance / (2f * default_distance);
  148. //}
  149. // 鼠标滚轮拉伸
  150. targetDistance -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
  151. if (targetDistance > maxDistance)
  152. {
  153. targetDistance = maxDistance;
  154. }
  155. }
  156. // 控制旋转角度范围:min max
  157. float ClampAngle(float angle, float min, float max)
  158. {
  159. // 控制旋转角度不超过360
  160. if (angle < -360f) angle += 360f;
  161. if (angle > 360f) angle -= 360f;
  162. return Mathf.Clamp(angle, min, max);
  163. }
  164. private void FixedUpdate()
  165. {
  166. rotation = Quaternion.Slerp(rotation, targetRotation, Time.deltaTime * rotateLerp);
  167. position = Vector3.Lerp(position, targetPosition, Time.deltaTime * moveLerp);
  168. distance = Mathf.Lerp(distance, targetDistance, Time.deltaTime * zoomLerp);
  169. // 设置摄像头旋转
  170. transform.rotation = rotation;
  171. // 设置摄像头位置
  172. transform.position = position - rotation * new Vector3(0, 0, distance);
  173. }
  174. public void InitModelPos()
  175. {
  176. InitCameraPos(allModelData);
  177. }
  178. public void InitCameraPos(FreeCameraData data)
  179. {
  180. if (data == null) return;
  181. currentCameraData = data;
  182. InitCameraPos();
  183. }
  184. /// <summary>
  185. /// 初始化摄像机围殴之
  186. /// </summary>
  187. public void InitCameraPos()
  188. {
  189. //// 旋转归零
  190. targetRotation = Quaternion.Euler(currentCameraData.targetRot);
  191. //// 初始位置是模型
  192. targetPosition = currentCameraData.model.position;
  193. //// 初始镜头拉伸
  194. targetDistance = currentCameraData.targetDistance;
  195. }
  196. public void SetPosAndRot(Vector3 pos, Quaternion rot)
  197. {
  198. transform.position = pos;
  199. transform.rotation = rot;
  200. }
  201. /// <summary>
  202. /// 检测是否点击UI
  203. /// </summary>
  204. /// <param name="mousePosition"></param>
  205. /// <returns></returns>
  206. private bool IsPointerOverGameObject(Vector2 mousePosition)
  207. {
  208. //创建一个点击事件
  209. PointerEventData eventData = new PointerEventData(EventSystem.current);
  210. eventData.position = mousePosition;
  211. List<RaycastResult> raycastResults = new List<RaycastResult>();
  212. //向点击位置发射一条射线,检测是否点击UI
  213. EventSystem.current.RaycastAll(eventData, raycastResults);
  214. if (raycastResults.Count > 0)
  215. {
  216. if (raycastResults.Count == 1 && raycastResults.Find(p => p.gameObject.CompareTag("UIIgnore")).gameObject != null)
  217. {
  218. return false;
  219. }
  220. return true;
  221. }
  222. else
  223. {
  224. return false;
  225. }
  226. }
  227. }