UltimateOrbitCamera.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. using System;
  2. using UnityEngine;
  3. public class UltimateOrbitCamera : MonoBehaviour
  4. {
  5. private void Awake()
  6. {
  7. this.targetDistance = this.distance;
  8. if (this.invertAxisX)
  9. {
  10. this.invertXValue = -1;
  11. }
  12. else
  13. {
  14. this.invertXValue = 1;
  15. }
  16. if (this.invertAxisY)
  17. {
  18. this.invertYValue = -1;
  19. }
  20. else
  21. {
  22. this.invertYValue = 1;
  23. }
  24. if (this.invertAxisZoom)
  25. {
  26. this.invertZoomValue = -1;
  27. }
  28. else
  29. {
  30. this.invertZoomValue = 1;
  31. }
  32. if (this.autoRotateOn)
  33. {
  34. this.autoRotateReverseValue = -1;
  35. }
  36. else
  37. {
  38. this.autoRotateReverseValue = 1;
  39. }
  40. this._transform = base.transform;
  41. Rigidbody component = base.GetComponent<Rigidbody>();
  42. if (component != null)
  43. {
  44. component.freezeRotation = true;
  45. }
  46. this.SetTransform(this.target.position, new Vector2(this.initialAngleX, this.initialAngleY), this.distance);
  47. }
  48. private void OnEnable()
  49. {
  50. if (!GameMain.Instance.VRMode)
  51. {
  52. UICamera.fallThrough = base.gameObject;
  53. }
  54. }
  55. private void OnDisable()
  56. {
  57. if (!GameMain.Instance.VRMode)
  58. {
  59. UICamera.fallThrough = null;
  60. }
  61. }
  62. private void OnHover(bool isOver)
  63. {
  64. if (!GameMain.Instance.VRMode)
  65. {
  66. this.m_bFallThrough = isOver;
  67. }
  68. }
  69. public void SetTransform(Vector3 f_vecTargetPosWorld, Vector2 f_vecRot, float f_fDistance)
  70. {
  71. Transform transform = this.target;
  72. this.mVelocity = f_vecTargetPosWorld;
  73. transform.position = f_vecTargetPosWorld;
  74. this._transform.rotation = Quaternion.identity;
  75. this._transform.Rotate(new Vector3(0f, f_vecRot.x, this.m_fRotZ), Space.World);
  76. this._transform.Rotate(new Vector3(f_vecRot.y, 0f, this.m_fRotZ), Space.Self);
  77. this.xNowRotate = (this.x = f_vecRot.x);
  78. this.yNowRotate = (this.y = f_vecRot.y);
  79. this.xVelocity = (this.yVelocity = 0f);
  80. this.position = this._transform.rotation * new Vector3(0f, 0f, -f_fDistance) + this.target.position;
  81. this._transform.position = this.position;
  82. }
  83. public void SetOffset(Vector3 f_posOffset, bool is_animation)
  84. {
  85. this.offsetTarget = f_posOffset;
  86. this.isForceMove = !is_animation;
  87. }
  88. public void SetTargetPos(Vector3 f_vecTargetPosWorld)
  89. {
  90. Transform transform = this.target;
  91. this.mVelocity = f_vecTargetPosWorld;
  92. transform.position = f_vecTargetPosWorld;
  93. }
  94. public void SetAroundAngle(Vector2 f_vecAngle)
  95. {
  96. this.SetTransform(this.target.position, f_vecAngle, this.distance);
  97. }
  98. public Vector2 GetAroundAngle()
  99. {
  100. return new Vector2(this.xNowRotate, this.yNowRotate);
  101. }
  102. public void SetDistance(float f_fDistance)
  103. {
  104. this.distance = f_fDistance;
  105. this.targetDistance = f_fDistance;
  106. }
  107. private void Update()
  108. {
  109. if (this.target != null)
  110. {
  111. Camera component = base.GetComponent<Camera>();
  112. if (this.autoRotateOn)
  113. {
  114. this.xVelocity += this.autoRotateSpeed * (float)this.autoRotateReverseValue * Time.deltaTime;
  115. }
  116. if (this.mouseControl && this.m_bFallThrough && 0f <= Input.mousePosition.x && Input.mousePosition.x < (float)Screen.width && 0f <= Input.mousePosition.y && Input.mousePosition.y < (float)Screen.height)
  117. {
  118. if (!this.clickToRotate || (this.leftClickToRotate && NInput.GetMouseButton(0)) || (this.rightClickToRotate && NInput.GetMouseButton(1)))
  119. {
  120. this.xVelocity += NInput.GetAxis(this.mouseAxisX) * this.xSpeed * (float)this.invertXValue;
  121. this.yVelocity -= NInput.GetAxis(this.mouseAxisY) * this.ySpeed * (float)this.invertYValue;
  122. this.spinning = false;
  123. }
  124. this.zoomVelocity -= NInput.GetAxis(this.mouseAxisZoom) * this.zoomSpeed * (float)this.invertZoomValue;
  125. }
  126. if (this.mouseControl && NInput.GetMouseButton(2) && this.m_bFallThrough)
  127. {
  128. Vector3 vector = new Vector3(-NInput.GetAxis(this.mouseAxisX), -NInput.GetAxis(this.mouseAxisY), 0f);
  129. Transform transform = this.target.transform;
  130. Transform transform2 = base.transform;
  131. this.mVelocity += (transform2.right * vector.x + transform2.up * vector.y) * this.moveSpeed;
  132. this.spinning = false;
  133. }
  134. if (this.mouseControl && NInput.GetMouseButton(2))
  135. {
  136. if (Input.GetKey(KeyCode.W))
  137. {
  138. this.mVelocity += component.transform.TransformDirection(Vector3.forward) * this.moveSpeed;
  139. }
  140. if (Input.GetKey(KeyCode.A))
  141. {
  142. this.mVelocity += component.transform.TransformDirection(Vector3.left) * this.moveSpeed;
  143. }
  144. if (Input.GetKey(KeyCode.S))
  145. {
  146. this.mVelocity += component.transform.TransformDirection(Vector3.back) * this.moveSpeed;
  147. }
  148. if (Input.GetKey(KeyCode.D))
  149. {
  150. this.mVelocity += component.transform.TransformDirection(Vector3.right) * this.moveSpeed;
  151. }
  152. }
  153. if (this.SpinEnabled && ((this.mouseControl && this.clickToRotate) || this.keyboardControl))
  154. {
  155. if ((this.spinUseAxis && NInput.GetAxis(this.spinAxis) != 0f) || (!this.spinUseAxis && Input.GetKey(this.spinKey)))
  156. {
  157. this.spinning = true;
  158. this.spinSpeed = Mathf.Min(this.xVelocity, this.maxSpinSpeed);
  159. }
  160. if (this.spinning)
  161. {
  162. this.xVelocity = this.spinSpeed;
  163. }
  164. }
  165. if (this.limitX)
  166. {
  167. if (this.x + this.xVelocity < this.xMinLimit + this.xLimitOffset)
  168. {
  169. this.xVelocity = this.xMinLimit + this.xLimitOffset - this.x;
  170. }
  171. else if (this.x + this.xVelocity > this.xMaxLimit + this.xLimitOffset)
  172. {
  173. this.xVelocity = this.xMaxLimit + this.xLimitOffset - this.x;
  174. }
  175. this.x += this.xVelocity;
  176. this._transform.Rotate(new Vector3(0f, this.xVelocity, 0f), Space.World);
  177. }
  178. else
  179. {
  180. this._transform.Rotate(new Vector3(0f, this.xVelocity, this.m_fRotZ - this.m_fBefRotZ), Space.World);
  181. this.m_fBefRotZ = this.m_fRotZ;
  182. }
  183. if (this.limitY)
  184. {
  185. if (this.y + this.yVelocity < this.yMinLimit + this.yLimitOffset)
  186. {
  187. this.yVelocity = this.yMinLimit + this.yLimitOffset - this.y;
  188. }
  189. else if (this.y + this.yVelocity > this.yMaxLimit + this.yLimitOffset)
  190. {
  191. this.yVelocity = this.yMaxLimit + this.yLimitOffset - this.y;
  192. }
  193. this.y += this.yVelocity;
  194. this._transform.Rotate(new Vector3(this.yVelocity, 0f, 0f), Space.Self);
  195. }
  196. else
  197. {
  198. this._transform.Rotate(new Vector3(this.yVelocity, 0f, this.m_fRotZ - this.m_fBefRotZ), Space.Self);
  199. this.m_fBefRotZ = this.m_fRotZ;
  200. }
  201. this.xNowRotate += this.xVelocity;
  202. this.yNowRotate += this.yVelocity;
  203. if (this.targetDistance + this.zoomVelocity < this.minDistance)
  204. {
  205. this.zoomVelocity = this.minDistance - this.targetDistance;
  206. }
  207. else if (this.targetDistance + this.zoomVelocity > this.maxDistance)
  208. {
  209. this.zoomVelocity = this.maxDistance - this.targetDistance;
  210. }
  211. this.targetDistance += this.zoomVelocity;
  212. this.distance = Mathf.Lerp(this.distance, this.targetDistance, this.smoothingZoom);
  213. this.target.position = Vector3.Lerp(this.target.position, this.mVelocity, this.smoothingMove);
  214. if (this.cameraCollision)
  215. {
  216. this.ray = new Ray(this.target.position, (this._transform.position - this.target.position).normalized);
  217. if (Physics.SphereCast(this.ray.origin, this.collisionRadius, this.ray.direction, out this.hit, this.distance))
  218. {
  219. this.distance = this.hit.distance;
  220. }
  221. }
  222. if (this.isForceMove)
  223. {
  224. this.offsetNow = this.offsetTarget;
  225. this.isForceMove = false;
  226. }
  227. else
  228. {
  229. this.offsetNow = Vector3.Lerp(this.offsetNow, this.offsetTarget, this.smoothingMove);
  230. }
  231. this.position = this._transform.rotation * new Vector3(0f, 0f, -this.distance) + this.target.position;
  232. this._transform.position = this.position;
  233. Vector3 vector2 = component.WorldToScreenPoint(this.target.position);
  234. Vector3 a = component.ScreenToWorldPoint(new Vector3(vector2.x, vector2.y, vector2.z));
  235. Vector3 b = component.ScreenToWorldPoint(new Vector3((float)Screen.width / 2f + this.offsetNow.x, (float)Screen.height / 2f, vector2.z));
  236. this._transform.position += a - b;
  237. if (!this.SpinEnabled || !this.spinning)
  238. {
  239. this.xVelocity *= this.dampeningX;
  240. }
  241. this.yVelocity *= this.dampeningY;
  242. this.zoomVelocity = 0f;
  243. }
  244. else
  245. {
  246. Debug.LogWarning("Orbit Cam - No Target Given");
  247. }
  248. }
  249. public void OnDrawGizmos()
  250. {
  251. Gizmos.DrawIcon(this.target.position, "gizmo_eye.png", true);
  252. if (base.enabled)
  253. {
  254. Gizmos.DrawRay(base.gameObject.transform.position, this.target.position - base.gameObject.transform.position);
  255. }
  256. }
  257. public Transform target;
  258. public float distance = 10f;
  259. public float maxDistance = 10f;
  260. public float minDistance = 5f;
  261. public bool mouseControl = true;
  262. public string mouseAxisX = "Mouse X";
  263. public string mouseAxisY = "Mouse Y";
  264. public string mouseAxisZoom = "Mouse ScrollWheel";
  265. public bool keyboardControl;
  266. public string kbPanAxisX = "Horizontal";
  267. public string kbPanAxisY = "Vertical";
  268. public bool kbUseZoomAxis;
  269. public KeyCode zoomInKey = KeyCode.R;
  270. public KeyCode zoomOutKey = KeyCode.F;
  271. public string kbZoomAxisName = string.Empty;
  272. public float initialAngleX;
  273. public float initialAngleY;
  274. public bool invertAxisX;
  275. public bool invertAxisY;
  276. public bool invertAxisZoom;
  277. public float xSpeed = 1f;
  278. public float ySpeed = 1f;
  279. public float zoomSpeed = 5f;
  280. public float moveSpeed = 1f;
  281. public float dampeningX = 0.9f;
  282. public float dampeningY = 0.9f;
  283. public float smoothingZoom = 0.1f;
  284. public float smoothingMove = 0.1f;
  285. public bool limitY = true;
  286. public float yMinLimit = -60f;
  287. public float yMaxLimit = 60f;
  288. public float yLimitOffset;
  289. public bool limitX;
  290. public float xMinLimit = -60f;
  291. public float xMaxLimit = 60f;
  292. public float xLimitOffset;
  293. public bool clickToRotate = true;
  294. public bool leftClickToRotate = true;
  295. public bool rightClickToRotate;
  296. public bool autoRotateOn;
  297. public bool autoRotateReverse;
  298. public float autoRotateSpeed = 0.1f;
  299. public bool SpinEnabled;
  300. public bool spinUseAxis;
  301. public KeyCode spinKey = KeyCode.LeftControl;
  302. public string spinAxis = string.Empty;
  303. public float maxSpinSpeed = 3f;
  304. private bool spinning;
  305. private float spinSpeed;
  306. public bool cameraCollision;
  307. public float collisionRadius = 0.25f;
  308. private float xVelocity;
  309. private float yVelocity;
  310. private Vector3 mVelocity;
  311. private float zoomVelocity;
  312. private float targetDistance = 10f;
  313. private float x;
  314. private float y;
  315. private Vector3 position;
  316. private float xNowRotate;
  317. private float yNowRotate;
  318. private bool isForceMove;
  319. public Vector3 offsetTarget;
  320. private Vector3 offsetNow;
  321. private bool m_bFallThrough;
  322. [HideInInspector]
  323. public int invertXValue = 1;
  324. [HideInInspector]
  325. public int invertYValue = 1;
  326. [HideInInspector]
  327. public int invertZoomValue = 1;
  328. [HideInInspector]
  329. public int autoRotateReverseValue = 1;
  330. public float m_fRotZ;
  331. private float m_fBefRotZ;
  332. private Ray ray;
  333. private RaycastHit hit;
  334. private Transform _transform;
  335. private enum MouseButtonDown
  336. {
  337. MBD_LEFT,
  338. MBD_RIGHT,
  339. MBD_MIDDLE
  340. }
  341. }