Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using System.Collections;
  5.  
  6. namespace HD
  7. {
  8. public class CameraController
  9. {
  10. #region Data
  11. const float moveSpeed = 0.3f;
  12. const float scroolSpeed = 0.06f;
  13. static readonly Vector3 closeWP = new Vector3(0, 2, 0);
  14. static readonly Vector3 farWP = new Vector3(0, 30, 0);
  15.  
  16. readonly UnityEngine.Transform cameraHolderTransform;
  17. readonly UnityEngine.Transform cameraTransform;
  18.  
  19. Vector3? lastMousePosition;
  20.  
  21. Vector3 targetCameraPosition;
  22. float targetScale = .5f;
  23. float currentScale = .5f;
  24. Quaternion targetHolderRotation, lasttargetHolderRotation, lastHook = Quaternion.identity;
  25. #endregion
  26.  
  27. #region Properties
  28. float deltaTime
  29. {
  30. get
  31. {
  32. return Time.unscaledDeltaTime;
  33. }
  34. }
  35. #endregion
  36.  
  37. #region Init
  38. public CameraController()
  39. {
  40. cameraHolderTransform = GameObject.Find("CameraHolder")?.transform;
  41. if(cameraHolderTransform == null)
  42. { // This is a unit test run
  43. return;
  44. }
  45.  
  46. cameraTransform = Camera.main.transform;
  47.  
  48. targetCameraPosition = cameraHolderTransform.position;
  49. lasttargetHolderRotation = targetHolderRotation = cameraHolderTransform.rotation;
  50.  
  51. lastMousePosition = Input.mousePosition;
  52. UpdateCameraTransform();
  53.  
  54. targetCameraPosition = cameraHolderTransform.position = Vector3.zero;
  55. }
  56. #endregion
  57.  
  58. #region Events
  59. public void OnUpdate()
  60. {
  61. if(cameraHolderTransform == null)
  62. {
  63. return;
  64. }
  65.  
  66. if(MoveCamera() | RotateCamera() | ZoomCamera())
  67. {
  68. }
  69. UpdateCameraTransform();
  70. Culler.instance.OnCameraChange();
  71.  
  72. lastMousePosition = Input.mousePosition;
  73. }
  74. #endregion
  75.  
  76. #region Private Write API
  77. void UpdateCameraTransform()
  78. {
  79. var lastScale = currentScale;
  80. currentScale = Mathf.Lerp(currentScale, targetScale, 3.5f * deltaTime);
  81.  
  82. Controllers.mouseController.RefreshMousePosition();
  83. Vector3? originalMousePosition = Controllers.mouseController.mousePosition;
  84.  
  85. cameraTransform.localPosition = Vector3.Lerp(closeWP, farWP, currentScale);
  86.  
  87. cameraTransform.localRotation = Quaternion.Lerp(cameraTransform.localRotation,
  88. targetHolderRotation * lastHook,
  89. (HDMath.Range(1 - currentScale, .1f, .8f)) * 20 * deltaTime);
  90.  
  91. if(currentScale > .2f)
  92. { // Attempt to hover over the same location
  93. Controllers.mouseController.RefreshMousePosition();
  94. Vector3? newMousePosition = Controllers.mouseController.mousePosition;
  95. if(newMousePosition != null && originalMousePosition != null)
  96. {
  97. Vector3 deltaMousePosition = originalMousePosition.Value - newMousePosition.Value;
  98.  
  99. deltaMousePosition *= 1.3f;
  100. targetCameraPosition += deltaMousePosition;
  101. cameraHolderTransform.position += deltaMousePosition;
  102. }
  103. }
  104.  
  105. lastHook = Quaternion.Euler(
  106. new Vector3(35 + 55 * Math.Min(1, 3f * currentScale), 0, 0));
  107. cameraTransform.localRotation = Quaternion.Lerp(cameraTransform.localRotation,
  108. targetHolderRotation * lastHook,
  109. (HDMath.Range(1 - currentScale, .1f, .8f)) * 20 * deltaTime);
  110.  
  111. cameraHolderTransform.localPosition
  112. = Vector3.Lerp(cameraHolderTransform.localPosition, targetCameraPosition, 7.5f * deltaTime);
  113.  
  114. lasttargetHolderRotation = targetHolderRotation;
  115. }
  116.  
  117. bool MoveCamera()
  118. {
  119. Vector3 moveVector = Vector3.zero;
  120. if(Input.IsDown(InputType.CameraForward))
  121. {
  122. moveVector += Vector3.forward;
  123. }
  124. if(Input.IsDown(InputType.CameraLeft))
  125. {
  126. moveVector += Vector3.left;
  127. }
  128. if(Input.IsDown(InputType.CameraBackward))
  129. {
  130. moveVector += Vector3.back;
  131. }
  132. if(Input.IsDown(InputType.CameraRight))
  133. {
  134. moveVector += Vector3.right;
  135. }
  136.  
  137. if(moveVector == Vector3.zero)
  138. {
  139. return false;
  140. }
  141.  
  142. targetCameraPosition += (targetHolderRotation * moveVector) * (currentScale * .85f + .15f) * 50f * deltaTime;
  143.  
  144. return true;
  145. }
  146.  
  147. bool RotateCamera()
  148. {
  149. if(Input.mousePosition == null || lastMousePosition == null)
  150. {
  151. return false;
  152. }
  153.  
  154. if(Input.IsDown(InputType.CameraRotate) == false)
  155. {
  156. return false;
  157. }
  158.  
  159. float deltaX = lastMousePosition.Value.x - Input.mousePosition.Value.x;
  160. float deltaY = Input.mousePosition.Value.y - lastMousePosition.Value.y;
  161. targetHolderRotation *= Quaternion.Euler(new Vector3(0, (deltaX + deltaY) * .5f * HDMath.Range(currentScale, .4f, .8f), 0));
  162.  
  163. return true;
  164. }
  165.  
  166. bool ZoomCamera()
  167. {
  168. if(EventSystem.current.IsPointerOverGameObject())
  169. {
  170. // we are hovering over a UI option
  171. return false;
  172. }
  173.  
  174. targetScale += UnityEngine.Input.mouseScrollDelta.y * scroolSpeed;
  175. if(targetScale == 0)
  176. {
  177. return false;
  178. }
  179.  
  180. if(targetScale > 1)
  181. {
  182. targetScale = 1;
  183. }
  184. if(targetScale < 0)
  185. {
  186. targetScale = 0;
  187. }
  188.  
  189.  
  190.  
  191. return true;
  192. }
  193. #endregion
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement