CameraController.cs 7.5 KB

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