CameraController.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 }
  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. offset = new Vector3(offset.x, offset.y, offset.z/*-Mathf.Abs(zoomMax) / 2*/);
  35. transform.position = target.position + offset;
  36. }
  37. void Update()
  38. {
  39. if(axes == RotationAxes.KeyBoard)KeyboardsCommands();
  40. else MouseInput();
  41. }
  42. void MouseInput()
  43. {
  44. if (EventSystem.current.IsPointerOverGameObject())
  45. {
  46. return;
  47. }
  48. if (Input.GetMouseButton(0))
  49. {
  50. }
  51. else if (Input.GetMouseButton(1))
  52. {
  53. MouseRightClick();
  54. }
  55. else if (Input.GetMouseButton(2))
  56. {
  57. MouseMiddleButtonClicked();
  58. }
  59. else if (Input.GetMouseButtonUp(1))
  60. {
  61. ShowAndUnlockCursor();
  62. }
  63. else if (Input.GetMouseButtonUp(2))
  64. {
  65. ShowAndUnlockCursor();
  66. }
  67. else
  68. {
  69. MouseWheeling();
  70. }
  71. }
  72. void ShowAndUnlockCursor()
  73. {
  74. Cursor.lockState = CursorLockMode.None;
  75. Cursor.visible = true;
  76. }
  77. void HideAndLockCursor()
  78. {
  79. Cursor.lockState = CursorLockMode.Locked;
  80. Cursor.visible = false;
  81. }
  82. void MouseMiddleButtonClicked()
  83. {
  84. HideAndLockCursor();
  85. Vector3 NewPosition = new Vector3(Input.GetAxis("Mouse X"), 0, Input.GetAxis("Mouse Y"));
  86. Vector3 pos = transform.position;
  87. if (NewPosition.x > 0.0f)
  88. {
  89. pos += transform.right;
  90. }
  91. else if (NewPosition.x < 0.0f)
  92. {
  93. pos -= transform.right;
  94. }
  95. if (NewPosition.z > 0.0f)
  96. {
  97. pos += transform.forward;
  98. }
  99. if (NewPosition.z < 0.0f)
  100. {
  101. pos -= transform.forward;
  102. }
  103. pos.y = transform.position.y;
  104. transform.position = pos;
  105. }
  106. void MouseRightClick()
  107. {
  108. HideAndLockCursor();
  109. switch (axes)
  110. {
  111. case RotationAxes.MouseXAndY:
  112. float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
  113. rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  114. rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
  115. transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
  116. break;
  117. case RotationAxes.MouseX:
  118. transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
  119. break;
  120. case RotationAxes.RelativeObject:
  121. /*if (Input.GetAxis("Mouse ScrollWheel") > 0)
  122. offset.z += zoom;
  123. else if (Input.GetAxis("Mouse ScrollWheel") < 0)
  124. offset.z -= zoom;*/
  125. //offset.z = Mathf.Clamp(offset.z, -Mathf.Abs(zoomMax), -Mathf.Abs(zoomMin));
  126. X = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivity;
  127. Y += Input.GetAxis("Mouse Y") * sensitivity;
  128. Y = Mathf.Clamp(Y, -limit, limit);
  129. transform.localEulerAngles = new Vector3(-Y, X, 0);
  130. transform.position = transform.localRotation * offset + target.position;
  131. break;
  132. default:
  133. rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  134. rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
  135. transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
  136. break;
  137. }
  138. }
  139. void MouseWheeling()
  140. {
  141. /*Vector3 pos = transform.position;
  142. if (Input.GetAxis("Mouse ScrollWheel") < 0)
  143. {
  144. pos = pos - transform.forward;
  145. transform.position = pos;
  146. }
  147. if (Input.GetAxis("Mouse ScrollWheel") > 0)
  148. {
  149. pos = pos + transform.forward;
  150. transform.position = pos;
  151. }*/
  152. if (Input.GetAxis("Mouse ScrollWheel") > 0)
  153. {
  154. offset.z += zoom;
  155. }
  156. else if (Input.GetAxis("Mouse ScrollWheel") < 0)
  157. offset.z -= zoom;
  158. offset.z = Mathf.Clamp(offset.z, -Mathf.Abs(zoomMax), -Mathf.Abs(zoomMin));
  159. transform.position = transform.localRotation * offset + target.position;
  160. }
  161. private Vector3 GetBaseInput()
  162. { //returns the basic values, if it's 0 than it's not active.
  163. Vector3 p_Velocity = new Vector3();
  164. if (Input.GetKey(KeyCode.W))
  165. {
  166. p_Velocity += new Vector3(0, 0, 1);
  167. }
  168. if (Input.GetKey(KeyCode.S))
  169. {
  170. p_Velocity += new Vector3(0, 0, -1);
  171. }
  172. if (Input.GetKey(KeyCode.A))
  173. {
  174. p_Velocity += new Vector3(-1, 0, 0);
  175. }
  176. if (Input.GetKey(KeyCode.D))
  177. {
  178. p_Velocity += new Vector3(1, 0, 0);
  179. }
  180. return p_Velocity;
  181. }
  182. private void KeyboardsCommands()
  183. {
  184. //Keyboard commands
  185. //float f = 0.0f;
  186. Vector3 p = GetBaseInput();
  187. p = p * Time.deltaTime;
  188. Vector3 newPosition = transform.position;
  189. if (Input.GetKey(KeyCode.Space))
  190. { //If player wants to move on X and Z axis only
  191. transform.Translate(p);
  192. newPosition.x = transform.position.x;
  193. newPosition.z = transform.position.z;
  194. transform.position = newPosition;
  195. }
  196. else
  197. {
  198. transform.Translate(p);
  199. }
  200. }
  201. }