CameraController.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. /// <summary>
  9. /// Отслеживание объекта
  10. /// </summary>
  11. public class CameraController : MonoBehaviour
  12. {
  13. public enum RotationAxes { Mouse, TrackMarker }
  14. public RotationAxes axes = RotationAxes.Mouse;
  15. public float movementSpeed = 0.1f;
  16. public int mouseSpeed = 100;
  17. public float smoothness = 0.85f;
  18. public Button ButtonCameraCenter;
  19. Vector3 targetPosition;
  20. private Vector3 dragOrigin;
  21. public Transform target;
  22. public GameObject location;
  23. public Toggle Toggle_projection;
  24. void Start()
  25. {
  26. targetPosition = transform.position;
  27. yaw = transform.eulerAngles.y;
  28. pitch = transform.eulerAngles.x;
  29. ButtonCameraCenter.onClick.AddListener(CameraCenter);
  30. }
  31. void Update()
  32. {
  33. if (!EventSystem.current.IsPointerOverGameObject())
  34. {
  35. switch (axes)
  36. {
  37. case RotationAxes.Mouse:
  38. //MouseCommands();
  39. MovementMouse();
  40. break;
  41. case RotationAxes.TrackMarker:
  42. TrackMarker();
  43. break;
  44. }
  45. }
  46. Camera.main.orthographic = Toggle_projection.isOn;
  47. }
  48. void ShowAndUnlockCursor()
  49. {
  50. Cursor.lockState = CursorLockMode.None;
  51. Cursor.visible = true;
  52. }
  53. void HideAndLockCursor()
  54. {
  55. Cursor.lockState = CursorLockMode.Locked;
  56. Cursor.visible = false;
  57. }
  58. private void MouseCommands()
  59. {
  60. //if (Input.GetKey(KeyCode.W))
  61. //{
  62. // targetPosition += transform.forward * movementSpeed;
  63. // Camera.main.orthographicSize++;
  64. //}
  65. if (Input.GetKey(KeyCode.LeftArrow))
  66. targetPosition -= transform.right * movementSpeed;
  67. //if (Input.GetKey(KeyCode.S))
  68. //{
  69. // targetPosition -= transform.forward * movementSpeed;
  70. // if (Camera.main.orthographicSize > 1) Camera.main.orthographicSize--;
  71. //}
  72. if (Input.GetKey(KeyCode.RightArrow))
  73. targetPosition += transform.right * movementSpeed;
  74. if (Input.GetKey(KeyCode.DownArrow))
  75. targetPosition -= transform.up * movementSpeed;
  76. if (Input.GetKey(KeyCode.UpArrow))
  77. targetPosition += transform.up * movementSpeed;
  78. if (Input.GetMouseButtonDown(2))
  79. {
  80. dragOrigin = Input.mousePosition;
  81. }
  82. if (Input.GetMouseButton(1))
  83. {
  84. Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
  85. if (pos.x < 0)
  86. targetPosition -= transform.right * movementSpeed * 0.1f;
  87. if (pos.x > 0)
  88. targetPosition += transform.right * movementSpeed * 0.1f;
  89. if (pos.y < 0)
  90. targetPosition -= transform.up * movementSpeed * 0.1f;
  91. if (pos.y > 0)
  92. targetPosition += transform.up * movementSpeed * 0.1f;
  93. }
  94. if (Input.GetAxis("Mouse ScrollWheel") > 0 && !EventSystem.current.IsPointerOverGameObject())
  95. {
  96. targetPosition += transform.forward * movementSpeed* mouseSpeed;
  97. if (Camera.main.orthographicSize > 1) Camera.main.orthographicSize--;
  98. }
  99. else if (Input.GetAxis("Mouse ScrollWheel") < 0 && !EventSystem.current.IsPointerOverGameObject())
  100. {
  101. targetPosition -= transform.forward * movementSpeed*mouseSpeed;
  102. Camera.main.orthographicSize++;
  103. }
  104. transform.position = Vector3.Lerp(transform.position, targetPosition, (1.0f - smoothness));
  105. }
  106. private Vector3 screenPoint;
  107. private Vector3 offset;
  108. private Vector3 curScreenPoint;
  109. private Vector3 curPosition;
  110. void OnMouseDown()
  111. {
  112. //Debug.Log(StagesEditorController.indexCursor);
  113. screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  114. offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  115. }
  116. private void OnMouseDrag()
  117. {
  118. switch (axes)
  119. {
  120. case RotationAxes.Mouse:
  121. curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  122. curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
  123. transform.position = curPosition;
  124. break;
  125. }
  126. }
  127. void TrackMarker()
  128. {
  129. transform.Translate(0, 0, Input.GetAxis("Mouse ScrollWheel") * zoomSpeed, Space.Self);
  130. var marker_offset = new Vector3(target.position.x, targetPosition.y, target.position.z);
  131. if(!target.GetComponent<Outline>().enabled) target.GetComponent<Outline>().enabled = true;
  132. targetPosition =Vector3.Lerp(transform.position, /*targetPosition + target.position - */marker_offset, (1.0f - smoothness));
  133. transform.position = targetPosition;
  134. if (Input.GetMouseButton(1))
  135. {
  136. target.GetComponent<Outline>().enabled = false;
  137. axes = RotationAxes.Mouse;
  138. var player = PlayerController.instance;
  139. if (player.UserInfo.activeSelf)
  140. {
  141. var client = Client.instance;
  142. client.ImageStreamStopSend(player.user_broadcast.Value);
  143. player.user_broadcast = null;
  144. player.UserInfo.SetActive(false);
  145. }
  146. }
  147. }
  148. public float lookSpeedH = 2f;
  149. public float lookSpeedV = 2f;
  150. public float zoomSpeed = 2f;
  151. public float dragSpeed = 1f;
  152. public float dragKeySpeed = 0.1f;
  153. private float yaw = 0f;
  154. private float pitch = 0f;
  155. void MovementMouse()
  156. {
  157. //Look around with Right Mouse
  158. if (Input.GetMouseButton(1))
  159. {
  160. yaw += lookSpeedH * Input.GetAxis("Mouse X");
  161. pitch -= lookSpeedV * Input.GetAxis("Mouse Y");
  162. transform.eulerAngles = new Vector3(pitch, yaw, 0f);
  163. }
  164. //drag camera around with Middle Mouse
  165. if (Input.GetMouseButton(2))
  166. {
  167. transform.Translate(-Input.GetAxisRaw("Mouse X") * dragSpeed, -Input.GetAxisRaw("Mouse Y") * dragSpeed, 0);
  168. }
  169. if (Toggle_projection.isOn)
  170. {
  171. if (Input.GetAxis("Mouse ScrollWheel") < 0)
  172. {
  173. if (Camera.main.orthographicSize > 1) Camera.main.orthographicSize--;
  174. }
  175. if (Input.GetAxis("Mouse ScrollWheel") > 0) Camera.main.orthographicSize++;
  176. }
  177. else
  178. //Zoom in and out with Mouse Wheel
  179. transform.Translate(0, 0, Input.GetAxis("Mouse ScrollWheel") * zoomSpeed, Space.Self);
  180. if (Input.GetKey(KeyCode.LeftArrow))
  181. //targetPosition -= transform.right * movementSpeed;
  182. transform.Translate(-dragKeySpeed, 0, 0);
  183. if (Input.GetKey(KeyCode.RightArrow))
  184. transform.Translate(dragKeySpeed, 0, 0);
  185. if (Input.GetKey(KeyCode.DownArrow))
  186. transform.Translate(0, -dragKeySpeed, 0);
  187. if (Input.GetKey(KeyCode.UpArrow))
  188. transform.Translate(0, dragKeySpeed, 0);
  189. }
  190. public void CameraCenter()
  191. {
  192. if (location != null)
  193. {
  194. var pos = location.transform.position;
  195. var scale = location.transform.localScale;
  196. var x = pos.x - scale.x / 2;
  197. float y;
  198. var z = pos.z - scale.z / 2;
  199. if (x >= z)
  200. y = scale.x * 8;
  201. else y = scale.z * 8;
  202. transform.position = new Vector3(x, y, z);
  203. }
  204. else
  205. transform.position = new Vector3(5, 12, 5);
  206. transform.rotation = Quaternion.Euler(90, 0, 0);
  207. targetPosition = transform.position;
  208. yaw = transform.eulerAngles.y;
  209. pitch = transform.eulerAngles.x;
  210. }
  211. }