CameraController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. /// <summary>
  6. /// Отслеживание объекта
  7. /// </summary>
  8. public class CameraController : MonoBehaviour
  9. {
  10. public enum RotationAxes { MouseXAndY, MouseX, MouseY, RelativeObject, KeyBoard, Mouse, TrackMarker }
  11. public RotationAxes axes = RotationAxes.MouseXAndY;
  12. public float sensitivityX = 2F;
  13. public float sensitivityY = 2F;
  14. public float minimumX = -360F;
  15. public float maximumX = 360F;
  16. public float minimumY = -90F;
  17. public float maximumY = 90F;
  18. float rotationY = -60F;
  19. // For camera movement
  20. //float CameraPanningSpeed = 10.0f;
  21. // Target Rotate
  22. public Transform target;
  23. public Vector3 offset;
  24. public float sensitivity = 3; // чувствительность мышки
  25. public float limit = 90; // ограничение вращения по Y
  26. public float zoom = 0.5f; // чувствительность при увеличении, колесиком мышки
  27. public float zoomMax = 50; // макс. увеличение
  28. public float zoomMin = 0.5f; // мин. увеличение
  29. private float X, Y;
  30. void Start()
  31. {
  32. limit = Mathf.Abs(limit);
  33. if (limit > 90) limit = 90;
  34. if (axes != RotationAxes.Mouse) offset = new Vector3(offset.x, offset.y, offset.z/*-Mathf.Abs(zoomMax) / 2*/);
  35. else
  36. offset = transform.position;
  37. if(target != null) transform.position = target.position + offset;
  38. targetPosition = transform.position;
  39. }
  40. void Update()
  41. {
  42. switch (axes)
  43. {
  44. case RotationAxes.KeyBoard:
  45. KeyboardsCommands();
  46. break;
  47. case RotationAxes.Mouse:
  48. MouseCommands();
  49. break;
  50. default:
  51. MouseInput();
  52. break;
  53. }
  54. }
  55. void MouseInput()
  56. {
  57. if (EventSystem.current.IsPointerOverGameObject())
  58. {
  59. return;
  60. }
  61. if (Input.GetMouseButton(0))
  62. {
  63. }
  64. else if (Input.GetMouseButton(1))
  65. {
  66. MouseRightClick();
  67. }
  68. else if (Input.GetMouseButton(2))
  69. {
  70. MouseMiddleButtonClicked();
  71. }
  72. else if (Input.GetMouseButtonUp(1))
  73. {
  74. ShowAndUnlockCursor();
  75. }
  76. else if (Input.GetMouseButtonUp(2))
  77. {
  78. ShowAndUnlockCursor();
  79. }
  80. else
  81. {
  82. MouseWheeling();
  83. }
  84. }
  85. void ShowAndUnlockCursor()
  86. {
  87. Cursor.lockState = CursorLockMode.None;
  88. Cursor.visible = true;
  89. }
  90. void HideAndLockCursor()
  91. {
  92. Cursor.lockState = CursorLockMode.Locked;
  93. Cursor.visible = false;
  94. }
  95. void MouseMiddleButtonClicked()
  96. {
  97. HideAndLockCursor();
  98. Vector3 NewPosition = new Vector3(Input.GetAxis("Mouse X"), 0, Input.GetAxis("Mouse Y"));
  99. Vector3 pos = transform.position;
  100. if (NewPosition.x > 0.0f)
  101. {
  102. pos += transform.right;
  103. }
  104. else if (NewPosition.x < 0.0f)
  105. {
  106. pos -= transform.right;
  107. }
  108. if (NewPosition.z > 0.0f)
  109. {
  110. pos += transform.forward;
  111. }
  112. if (NewPosition.z < 0.0f)
  113. {
  114. pos -= transform.forward;
  115. }
  116. pos.y = transform.position.y;
  117. transform.position = pos;
  118. }
  119. void MouseRightClick()
  120. {
  121. HideAndLockCursor();
  122. switch (axes)
  123. {
  124. case RotationAxes.MouseXAndY:
  125. float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
  126. rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  127. rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
  128. transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
  129. break;
  130. case RotationAxes.MouseX:
  131. transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
  132. break;
  133. case RotationAxes.RelativeObject:
  134. /*if (Input.GetAxis("Mouse ScrollWheel") > 0)
  135. offset.z += zoom;
  136. else if (Input.GetAxis("Mouse ScrollWheel") < 0)
  137. offset.z -= zoom;*/
  138. //offset.z = Mathf.Clamp(offset.z, -Mathf.Abs(zoomMax), -Mathf.Abs(zoomMin));
  139. X = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivity;
  140. Y += Input.GetAxis("Mouse Y") * sensitivity;
  141. Y = Mathf.Clamp(Y, -limit, limit);
  142. transform.localEulerAngles = new Vector3(-Y, X, 0);
  143. transform.position = transform.localRotation * offset + target.position;
  144. break;
  145. default:
  146. rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  147. rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
  148. transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
  149. break;
  150. }
  151. }
  152. void MouseWheeling()
  153. {
  154. /*Vector3 pos = transform.position;
  155. if (Input.GetAxis("Mouse ScrollWheel") < 0)
  156. {
  157. pos = pos - transform.forward;
  158. transform.position = pos;
  159. }
  160. if (Input.GetAxis("Mouse ScrollWheel") > 0)
  161. {
  162. pos = pos + transform.forward;
  163. transform.position = pos;
  164. }*/
  165. if (Input.GetAxis("Mouse ScrollWheel") > 0)
  166. {
  167. offset.z += zoom;
  168. }
  169. else if (Input.GetAxis("Mouse ScrollWheel") < 0)
  170. offset.z -= zoom;
  171. offset.z = Mathf.Clamp(offset.z, -Mathf.Abs(zoomMax), -Mathf.Abs(zoomMin));
  172. transform.position = transform.localRotation * offset + target.position;
  173. }
  174. private Vector3 GetBaseInput()
  175. { //returns the basic values, if it's 0 than it's not active.
  176. Vector3 p_Velocity = new Vector3();
  177. if (Input.GetKey(KeyCode.W))
  178. {
  179. p_Velocity += new Vector3(0, 0, 1);
  180. }
  181. if (Input.GetKey(KeyCode.S))
  182. {
  183. p_Velocity += new Vector3(0, 0, -1);
  184. }
  185. if (Input.GetKey(KeyCode.A))
  186. {
  187. p_Velocity += new Vector3(-1, 0, 0);
  188. }
  189. if (Input.GetKey(KeyCode.D))
  190. {
  191. p_Velocity += new Vector3(1, 0, 0);
  192. }
  193. return p_Velocity;
  194. }
  195. private void KeyboardsCommands()
  196. {
  197. //Keyboard commands
  198. //float f = 0.0f;
  199. Vector3 p = GetBaseInput();
  200. p = p * Time.deltaTime;
  201. Vector3 newPosition = transform.position;
  202. if (Input.GetKey(KeyCode.Space))
  203. { //If player wants to move on X and Z axis only
  204. transform.Translate(p);
  205. newPosition.x = transform.position.x;
  206. newPosition.z = transform.position.z;
  207. transform.position = newPosition;
  208. }
  209. else
  210. {
  211. transform.Translate(p);
  212. }
  213. }
  214. public float movementSpeed = 0.1f;
  215. public float rotationSpeed = 4f;
  216. public float smoothness = 0.85f;
  217. Vector3 targetPosition;
  218. public Quaternion targetRotation;
  219. float targetRotationY;
  220. float targetRotationX;
  221. public float dragSpeed = 0.00001f;
  222. private Vector3 dragOrigin;
  223. private void MouseCommands()
  224. {
  225. if (Input.GetKey(KeyCode.W))
  226. targetPosition += transform.forward * movementSpeed;
  227. if (Input.GetKey(KeyCode.A))
  228. targetPosition -= transform.right * movementSpeed;
  229. if (Input.GetKey(KeyCode.S))
  230. targetPosition -= transform.forward * movementSpeed;
  231. if (Input.GetKey(KeyCode.D))
  232. targetPosition += transform.right * movementSpeed;
  233. if (Input.GetKey(KeyCode.Q))
  234. targetPosition -= transform.up * movementSpeed;
  235. if (Input.GetKey(KeyCode.E))
  236. targetPosition += transform.up * movementSpeed;
  237. //
  238. if (Input.GetMouseButtonDown(2))
  239. {
  240. dragOrigin = Input.mousePosition;
  241. //return;
  242. }
  243. //if (!Input.GetMouseButton(2)) return;
  244. if (Input.GetMouseButton(1))
  245. {
  246. Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
  247. if(pos.x < 0)
  248. targetPosition -= transform.right * movementSpeed * 0.1f;
  249. if (pos.x>0)
  250. targetPosition += transform.right * movementSpeed * 0.1f;
  251. if (pos.y > 0)
  252. targetPosition -= transform.up * movementSpeed*0.1f;
  253. if (pos.y < 0)
  254. targetPosition += transform.up * movementSpeed * 0.1f;
  255. }
  256. if (Input.GetAxis("Mouse ScrollWheel") > 0)
  257. {
  258. //offset.z += zoom;
  259. targetPosition += transform.forward * movementSpeed;
  260. }
  261. else if (Input.GetAxis("Mouse ScrollWheel") < 0)
  262. {
  263. //offset.z -= zoom;
  264. targetPosition -= transform.forward * movementSpeed;
  265. }
  266. //offset.z = Mathf.Clamp(offset.z, -Mathf.Abs(zoomMax), -Mathf.Abs(zoomMin));
  267. //if (Input.GetAxis("Mouse ScrollWheel") != 0)
  268. //{
  269. // X = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivity;
  270. // Y += Input.GetAxis("Mouse Y") * sensitivity;
  271. // Y = Mathf.Clamp(Y, -limit, limit);
  272. // //transform.localEulerAngles = new Vector3(-Y, X, 0);
  273. // transform.position = new Vector3(transform.position.x, Y, transform.position.z);
  274. //}
  275. //transform.position = transform.localRotation * offset;
  276. //if (Input.GetMouseButton(1))
  277. //{
  278. // Cursor.visible = false;
  279. // targetRotationY += Input.GetAxis("Mouse X") * rotationSpeed;
  280. // targetRotationX -= Input.GetAxis("Mouse Y") * rotationSpeed;
  281. // targetRotation = Quaternion.Euler(targetRotationX, targetRotationY, 0.0f);
  282. //}
  283. //else
  284. // Cursor.visible = true;
  285. transform.position = Vector3.Lerp(transform.position, targetPosition, (1.0f - smoothness));
  286. //transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, (1.0f - smoothness));
  287. }
  288. }