CameraController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TMPro.Examples
  4. {
  5. public class CameraController : MonoBehaviour
  6. {
  7. public enum CameraModes { Follow, Isometric, Free }
  8. private Transform cameraTransform;
  9. private Transform dummyTarget;
  10. public Transform CameraTarget;
  11. public float FollowDistance = 30.0f;
  12. public float MaxFollowDistance = 100.0f;
  13. public float MinFollowDistance = 2.0f;
  14. public float ElevationAngle = 30.0f;
  15. public float MaxElevationAngle = 85.0f;
  16. public float MinElevationAngle = 0f;
  17. public float OrbitalAngle = 0f;
  18. public CameraModes CameraMode = CameraModes.Follow;
  19. public bool MovementSmoothing = true;
  20. public bool RotationSmoothing = false;
  21. private bool previousSmoothing;
  22. public float MovementSmoothingValue = 25f;
  23. public float RotationSmoothingValue = 5.0f;
  24. public float MoveSensitivity = 2.0f;
  25. private Vector3 currentVelocity = Vector3.zero;
  26. private Vector3 desiredPosition;
  27. private float mouseX;
  28. private float mouseY;
  29. private Vector3 moveVector;
  30. private float mouseWheel;
  31. // Controls for Touches on Mobile devices
  32. //private float prev_ZoomDelta;
  33. private const string event_SmoothingValue = "Slider - Smoothing Value";
  34. private const string event_FollowDistance = "Slider - Camera Zoom";
  35. void Awake()
  36. {
  37. if (QualitySettings.vSyncCount > 0)
  38. Application.targetFrameRate = 60;
  39. else
  40. Application.targetFrameRate = -1;
  41. if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android)
  42. Input.simulateMouseWithTouches = false;
  43. cameraTransform = transform;
  44. previousSmoothing = MovementSmoothing;
  45. }
  46. // Use this for initialization
  47. void Start()
  48. {
  49. if (CameraTarget == null)
  50. {
  51. // If we don't have a target (assigned by the player, create a dummy in the center of the scene).
  52. dummyTarget = new GameObject("Camera Target").transform;
  53. CameraTarget = dummyTarget;
  54. }
  55. }
  56. // Update is called once per frame
  57. void LateUpdate()
  58. {
  59. GetPlayerInput();
  60. // Check if we still have a valid target
  61. if (CameraTarget != null)
  62. {
  63. if (CameraMode == CameraModes.Isometric)
  64. {
  65. desiredPosition = CameraTarget.position + Quaternion.Euler(ElevationAngle, OrbitalAngle, 0f) * new Vector3(0, 0, -FollowDistance);
  66. }
  67. else if (CameraMode == CameraModes.Follow)
  68. {
  69. desiredPosition = CameraTarget.position + CameraTarget.TransformDirection(Quaternion.Euler(ElevationAngle, OrbitalAngle, 0f) * (new Vector3(0, 0, -FollowDistance)));
  70. }
  71. else
  72. {
  73. // Free Camera implementation
  74. }
  75. if (MovementSmoothing == true)
  76. {
  77. // Using Smoothing
  78. cameraTransform.position = Vector3.SmoothDamp(cameraTransform.position, desiredPosition, ref currentVelocity, MovementSmoothingValue * Time.fixedDeltaTime);
  79. //cameraTransform.position = Vector3.Lerp(cameraTransform.position, desiredPosition, Time.deltaTime * 5.0f);
  80. }
  81. else
  82. {
  83. // Not using Smoothing
  84. cameraTransform.position = desiredPosition;
  85. }
  86. if (RotationSmoothing == true)
  87. cameraTransform.rotation = Quaternion.Lerp(cameraTransform.rotation, Quaternion.LookRotation(CameraTarget.position - cameraTransform.position), RotationSmoothingValue * Time.deltaTime);
  88. else
  89. {
  90. cameraTransform.LookAt(CameraTarget);
  91. }
  92. }
  93. }
  94. void GetPlayerInput()
  95. {
  96. moveVector = Vector3.zero;
  97. // Check Mouse Wheel Input prior to Shift Key so we can apply multiplier on Shift for Scrolling
  98. mouseWheel = Input.GetAxis("Mouse ScrollWheel");
  99. float touchCount = Input.touchCount;
  100. if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift) || touchCount > 0)
  101. {
  102. mouseWheel *= 10;
  103. if (Input.GetKeyDown(KeyCode.I))
  104. CameraMode = CameraModes.Isometric;
  105. if (Input.GetKeyDown(KeyCode.F))
  106. CameraMode = CameraModes.Follow;
  107. if (Input.GetKeyDown(KeyCode.S))
  108. MovementSmoothing = !MovementSmoothing;
  109. // Check for right mouse button to change camera follow and elevation angle
  110. if (Input.GetMouseButton(1))
  111. {
  112. mouseY = Input.GetAxis("Mouse Y");
  113. mouseX = Input.GetAxis("Mouse X");
  114. if (mouseY > 0.01f || mouseY < -0.01f)
  115. {
  116. ElevationAngle -= mouseY * MoveSensitivity;
  117. // Limit Elevation angle between min & max values.
  118. ElevationAngle = Mathf.Clamp(ElevationAngle, MinElevationAngle, MaxElevationAngle);
  119. }
  120. if (mouseX > 0.01f || mouseX < -0.01f)
  121. {
  122. OrbitalAngle += mouseX * MoveSensitivity;
  123. if (OrbitalAngle > 360)
  124. OrbitalAngle -= 360;
  125. if (OrbitalAngle < 0)
  126. OrbitalAngle += 360;
  127. }
  128. }
  129. // Get Input from Mobile Device
  130. if (touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
  131. {
  132. Vector2 deltaPosition = Input.GetTouch(0).deltaPosition;
  133. // Handle elevation changes
  134. if (deltaPosition.y > 0.01f || deltaPosition.y < -0.01f)
  135. {
  136. ElevationAngle -= deltaPosition.y * 0.1f;
  137. // Limit Elevation angle between min & max values.
  138. ElevationAngle = Mathf.Clamp(ElevationAngle, MinElevationAngle, MaxElevationAngle);
  139. }
  140. // Handle left & right
  141. if (deltaPosition.x > 0.01f || deltaPosition.x < -0.01f)
  142. {
  143. OrbitalAngle += deltaPosition.x * 0.1f;
  144. if (OrbitalAngle > 360)
  145. OrbitalAngle -= 360;
  146. if (OrbitalAngle < 0)
  147. OrbitalAngle += 360;
  148. }
  149. }
  150. // Check for left mouse button to select a new CameraTarget or to reset Follow position
  151. if (Input.GetMouseButton(0))
  152. {
  153. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  154. RaycastHit hit;
  155. if (Physics.Raycast(ray, out hit, 300, 1 << 10 | 1 << 11 | 1 << 12 | 1 << 14))
  156. {
  157. if (hit.transform == CameraTarget)
  158. {
  159. // Reset Follow Position
  160. OrbitalAngle = 0;
  161. }
  162. else
  163. {
  164. CameraTarget = hit.transform;
  165. OrbitalAngle = 0;
  166. MovementSmoothing = previousSmoothing;
  167. }
  168. }
  169. }
  170. if (Input.GetMouseButton(2))
  171. {
  172. if (dummyTarget == null)
  173. {
  174. // We need a Dummy Target to anchor the Camera
  175. dummyTarget = new GameObject("Camera Target").transform;
  176. dummyTarget.position = CameraTarget.position;
  177. dummyTarget.rotation = CameraTarget.rotation;
  178. CameraTarget = dummyTarget;
  179. previousSmoothing = MovementSmoothing;
  180. MovementSmoothing = false;
  181. }
  182. else if (dummyTarget != CameraTarget)
  183. {
  184. // Move DummyTarget to CameraTarget
  185. dummyTarget.position = CameraTarget.position;
  186. dummyTarget.rotation = CameraTarget.rotation;
  187. CameraTarget = dummyTarget;
  188. previousSmoothing = MovementSmoothing;
  189. MovementSmoothing = false;
  190. }
  191. mouseY = Input.GetAxis("Mouse Y");
  192. mouseX = Input.GetAxis("Mouse X");
  193. moveVector = cameraTransform.TransformDirection(mouseX, mouseY, 0);
  194. dummyTarget.Translate(-moveVector, Space.World);
  195. }
  196. }
  197. // Check Pinching to Zoom in - out on Mobile device
  198. if (touchCount == 2)
  199. {
  200. Touch touch0 = Input.GetTouch(0);
  201. Touch touch1 = Input.GetTouch(1);
  202. Vector2 touch0PrevPos = touch0.position - touch0.deltaPosition;
  203. Vector2 touch1PrevPos = touch1.position - touch1.deltaPosition;
  204. float prevTouchDelta = (touch0PrevPos - touch1PrevPos).magnitude;
  205. float touchDelta = (touch0.position - touch1.position).magnitude;
  206. float zoomDelta = prevTouchDelta - touchDelta;
  207. if (zoomDelta > 0.01f || zoomDelta < -0.01f)
  208. {
  209. FollowDistance += zoomDelta * 0.25f;
  210. // Limit FollowDistance between min & max values.
  211. FollowDistance = Mathf.Clamp(FollowDistance, MinFollowDistance, MaxFollowDistance);
  212. }
  213. }
  214. // Check MouseWheel to Zoom in-out
  215. if (mouseWheel < -0.01f || mouseWheel > 0.01f)
  216. {
  217. FollowDistance -= mouseWheel * 5.0f;
  218. // Limit FollowDistance between min & max values.
  219. FollowDistance = Mathf.Clamp(FollowDistance, MinFollowDistance, MaxFollowDistance);
  220. }
  221. }
  222. }
  223. }