CameraRotateAround.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using UnityEngine;
  2. using System.Collections;
  3. ///
  4. public class CameraRotateAround : MonoBehaviour
  5. {
  6. public enum CameraState
  7. {
  8. /// <summary>
  9. /// 战斗默认
  10. /// </summary>
  11. BattleDefault,
  12. /// <summary>
  13. /// 战斗胜利
  14. /// </summary>
  15. BattleWin,
  16. /// <summary>
  17. /// 角色准备
  18. /// </summary>
  19. Ready,
  20. /// <summary>
  21. /// 相机修正(技能相机特效完毕后,同步主相机到最后帧的相机位置)
  22. /// </summary>
  23. Correct,
  24. /// <summary>
  25. /// 自由移动
  26. /// </summary>
  27. Free
  28. }
  29. /// <summary>
  30. /// 是否打包模式
  31. /// </summary>
  32. public bool IsAndroidAPK = false;
  33. /// <summary>
  34. /// 主相机要围绕其旋转的物体
  35. /// </summary>
  36. public Transform target;//
  37. /// <summary>
  38. /// 主相机与目标物体之间的距离
  39. /// </summary>
  40. public float distance = 7.0f;//
  41. ///*****************************************水平滚动相关
  42. /// <summary>
  43. /// 主相机水平方向旋转速度
  44. /// </summary>
  45. public float xSpeed = 70.0f;
  46. /// <summary>
  47. /// 主相机纵向旋转速度
  48. /// </summary>
  49. public float ySpeed = 70.0f;
  50. ///****************************************垂直滚动相关
  51. /// <summary>
  52. /// 最大y(单位是角度)
  53. /// </summary>
  54. public int yMaxLimit = 60;
  55. /// <summary>
  56. /// 最小y(单位是角度)
  57. /// </summary>
  58. public int yMinLimit = 10;
  59. /// <summary>
  60. /// 旋转衰减
  61. /// </summary>
  62. public float RotationDamping = 10.0f;
  63. /// <summary>
  64. /// 位置衰减
  65. /// </summary>
  66. public float PosDamping = 5.0f;
  67. /// <summary>
  68. /// 高度
  69. /// </summary>
  70. public float Height = 0f;
  71. /// <summary>
  72. /// 当前相机状态
  73. /// </summary>
  74. public CameraState mState = CameraState.Ready;
  75. /// <summary>
  76. /// 当前方向
  77. /// </summary>
  78. private Vector3 mWantedPosition = Vector3.zero;
  79. /// <summary>
  80. /// 当前角度
  81. /// </summary>
  82. private Quaternion mWantedRotation = Quaternion.identity;
  83. /// <summary>
  84. ///
  85. /// </summary>
  86. private float eulerAngles_x;
  87. /// <summary>
  88. ///
  89. /// </summary>
  90. private float eulerAngles_y;
  91. /// <summary>
  92. ///
  93. /// </summary>
  94. public Touch touch;
  95. /// <summary>
  96. /// 实例
  97. /// </summary>
  98. public static CameraRotateAround Instance = null;
  99. /// <summary>
  100. /// Awake
  101. /// </summary>
  102. void Awake()
  103. {
  104. Instance = this;
  105. }
  106. /// <summary>
  107. /// Start
  108. /// </summary>
  109. void Start()
  110. {
  111. ChangeState(mState);
  112. }
  113. /// <summary>
  114. /// 修改状态
  115. /// </summary>
  116. /// <param name="_state"></param>
  117. public void ChangeState(CameraState _state)
  118. {
  119. ResetAngle();
  120. switch (_state)
  121. {
  122. case CameraState.BattleDefault:
  123. mWantedPosition = target.TransformPoint(-6.0f, 6.0f, 14.1f);
  124. mWantedRotation = Quaternion.Euler(23.1f, 154.6f, 0);
  125. Invoke("LateFree", 1f);
  126. break;
  127. case CameraState.BattleWin:
  128. mWantedPosition = target.TransformPoint(0f, 1.14f, -1.48f);
  129. mWantedRotation = Quaternion.Euler(-9.8f, 0, 0);
  130. break;
  131. case CameraState.Ready:
  132. mWantedPosition = target.TransformPoint(0f, 3.0f, -2.0f);
  133. mWantedRotation = Quaternion.Euler(10, 0, 0);
  134. break;
  135. case CameraState.Correct:
  136. Invoke("LateFree", 1f);
  137. break;
  138. case CameraState.Free:
  139. ResetAngle();
  140. break;
  141. }
  142. mState = _state;
  143. }
  144. /// <summary>
  145. /// 延迟修改为自由状态
  146. /// </summary>
  147. private void LateFree()
  148. {
  149. ChangeState(CameraState.Free);
  150. }
  151. /// <summary>
  152. /// 修正摄像机位置
  153. /// </summary>
  154. public void Correct(Vector3 pos, Quaternion rotate)
  155. {
  156. mWantedPosition = target.TransformPoint(pos);
  157. mWantedRotation = rotate;
  158. ChangeState(CameraState.Correct);
  159. }
  160. /// <summary>
  161. ///
  162. /// </summary>
  163. private void ResetAngle()
  164. {
  165. ///当前物体的欧拉角
  166. Vector3 eulerAngles = this.transform.eulerAngles;
  167. this.eulerAngles_x = eulerAngles.y;
  168. this.eulerAngles_y = eulerAngles.x;
  169. }
  170. /// <summary>
  171. ///
  172. /// </summary>
  173. private void Update()
  174. {
  175. if (Input.GetKeyDown(KeyCode.W))
  176. {
  177. ChangeState(CameraState.BattleWin);
  178. }
  179. }
  180. /// <summary>
  181. /// LateUpdate
  182. /// </summary>
  183. private void LateUpdate()
  184. {
  185. if (this.target == null)
  186. {
  187. return;
  188. }
  189. if (mState != CameraState.Free)
  190. {
  191. //Quaternion mWantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
  192. transform.rotation = Quaternion.Slerp(transform.rotation, mWantedRotation, Time.deltaTime * RotationDamping);
  193. transform.position = Vector3.Lerp(transform.position, mWantedPosition, Time.deltaTime * PosDamping);
  194. return;
  195. }
  196. if (IsAndroidAPK)
  197. {
  198. if ((Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved))
  199. {
  200. touch = Input.GetTouch(0);
  201. this.eulerAngles_x += ((touch.deltaPosition.x * this.xSpeed) * this.distance) * 0.02f;
  202. this.eulerAngles_y -= (touch.deltaPosition.y * this.ySpeed) * 0.02f;
  203. this.eulerAngles_y = ClampAngle(this.eulerAngles_y, (float)this.yMinLimit, (float)this.yMaxLimit);
  204. Quaternion quaternion = Quaternion.Euler(this.eulerAngles_y, this.eulerAngles_x, (float)0);
  205. Vector3 vector = ((Vector3)(quaternion * new Vector3((float)0, (float)0, -this.distance))) + this.target.position;
  206. ///更改主相机的旋转角度和位置
  207. this.transform.rotation = quaternion;
  208. this.transform.position = vector;
  209. return;
  210. }
  211. }
  212. else
  213. {
  214. if (Input.GetMouseButton(0))
  215. {
  216. this.eulerAngles_x += ((Input.GetAxis("Mouse X") * this.xSpeed) * this.distance) * 0.02f;
  217. this.eulerAngles_y -= (Input.GetAxis("Mouse Y") * this.ySpeed) * 0.02f;
  218. this.eulerAngles_y = ClampAngle(this.eulerAngles_y, (float)this.yMinLimit, (float)this.yMaxLimit);
  219. Quaternion quaternion = Quaternion.Euler(this.eulerAngles_y, this.eulerAngles_x, (float)0);
  220. Vector3 vector = ((Vector3)(quaternion * new Vector3((float)0, (float)0, -this.distance))) + this.target.position;
  221. ///更改主相机的旋转角度和位置
  222. this.transform.rotation = quaternion;
  223. this.transform.position = vector;
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// 把角度限制到给定范围内
  229. /// </summary>
  230. /// <param name="angle"></param>
  231. /// <param name="min"></param>
  232. /// <param name="max"></param>
  233. /// <returns></returns>
  234. private float ClampAngle(float angle, float min, float max)
  235. {
  236. while (angle < -360)
  237. {
  238. angle += 360;
  239. }
  240. while (angle > 360)
  241. {
  242. angle -= 360;
  243. }
  244. return Mathf.Clamp(angle, min, max);
  245. }
  246. }