CameraController.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. /// <summary>
  7. /// Отслеживание объекта
  8. /// </summary>
  9. public class CameraController : MonoBehaviour
  10. {
  11. public enum RotationAxes { Mouse, TrackMarker }
  12. public RotationAxes axes = RotationAxes.Mouse;
  13. public float movementSpeed = 0.1f;
  14. public int mouseSpeed = 100;
  15. public float smoothness = 0.85f;
  16. Vector3 targetPosition;
  17. private Vector3 dragOrigin;
  18. public Transform target;
  19. void Start()
  20. {
  21. targetPosition = transform.position;
  22. }
  23. void Update()
  24. {
  25. //if (!EventSystem.current.IsPointerOverGameObject())
  26. //{
  27. switch (axes)
  28. {
  29. case RotationAxes.Mouse:
  30. MouseCommands();
  31. break;
  32. case RotationAxes.TrackMarker:
  33. TrackMarker();
  34. break;
  35. }
  36. //}
  37. }
  38. void ShowAndUnlockCursor()
  39. {
  40. Cursor.lockState = CursorLockMode.None;
  41. Cursor.visible = true;
  42. }
  43. void HideAndLockCursor()
  44. {
  45. Cursor.lockState = CursorLockMode.Locked;
  46. Cursor.visible = false;
  47. }
  48. private void MouseCommands()
  49. {
  50. //if (Input.GetKey(KeyCode.W))
  51. //{
  52. // targetPosition += transform.forward * movementSpeed;
  53. // Camera.main.orthographicSize++;
  54. //}
  55. if (Input.GetKey(KeyCode.LeftArrow))
  56. targetPosition -= transform.right * movementSpeed;
  57. //if (Input.GetKey(KeyCode.S))
  58. //{
  59. // targetPosition -= transform.forward * movementSpeed;
  60. // if (Camera.main.orthographicSize > 1) Camera.main.orthographicSize--;
  61. //}
  62. if (Input.GetKey(KeyCode.RightArrow))
  63. targetPosition += transform.right * movementSpeed;
  64. if (Input.GetKey(KeyCode.DownArrow))
  65. targetPosition -= transform.up * movementSpeed;
  66. if (Input.GetKey(KeyCode.UpArrow))
  67. targetPosition += transform.up * movementSpeed;
  68. if (Input.GetMouseButtonDown(2))
  69. {
  70. dragOrigin = Input.mousePosition;
  71. }
  72. if (Input.GetMouseButton(1))
  73. {
  74. Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
  75. if (pos.x < 0)
  76. targetPosition -= transform.right * movementSpeed * 0.1f;
  77. if (pos.x > 0)
  78. targetPosition += transform.right * movementSpeed * 0.1f;
  79. if (pos.y < 0)
  80. targetPosition -= transform.up * movementSpeed * 0.1f;
  81. if (pos.y > 0)
  82. targetPosition += transform.up * movementSpeed * 0.1f;
  83. }
  84. if (Input.GetAxis("Mouse ScrollWheel") > 0 && !EventSystem.current.IsPointerOverGameObject())
  85. {
  86. targetPosition += transform.forward * movementSpeed* mouseSpeed;
  87. if (Camera.main.orthographicSize > 1) Camera.main.orthographicSize--;
  88. }
  89. else if (Input.GetAxis("Mouse ScrollWheel") < 0 && !EventSystem.current.IsPointerOverGameObject())
  90. {
  91. targetPosition -= transform.forward * movementSpeed*mouseSpeed;
  92. Camera.main.orthographicSize++;
  93. }
  94. transform.position = Vector3.Lerp(transform.position, targetPosition, (1.0f - smoothness));
  95. }
  96. private Vector3 screenPoint;
  97. private Vector3 offset;
  98. private Vector3 curScreenPoint;
  99. private Vector3 curPosition;
  100. void OnMouseDown()
  101. {
  102. //Debug.Log(StagesEditorController.indexCursor);
  103. screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  104. offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  105. }
  106. private void OnMouseDrag()
  107. {
  108. switch (axes)
  109. {
  110. case RotationAxes.Mouse:
  111. curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  112. curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
  113. transform.position = curPosition;
  114. break;
  115. }
  116. }
  117. void TrackMarker()
  118. {
  119. //if (Input.GetKey(KeyCode.W))
  120. //{
  121. // targetPosition += transform.forward * movementSpeed;
  122. // Camera.main.orthographicSize++;
  123. //}
  124. //if (Input.GetKey(KeyCode.S))
  125. //{
  126. // targetPosition -= transform.forward * movementSpeed;
  127. // if (Camera.main.orthographicSize > 1) Camera.main.orthographicSize--;
  128. //}
  129. if (Input.GetAxis("Mouse ScrollWheel") > 0 && !EventSystem.current.IsPointerOverGameObject())
  130. {
  131. targetPosition += transform.forward * movementSpeed* mouseSpeed;
  132. if(Camera.main.orthographicSize > 1) Camera.main.orthographicSize--;
  133. }
  134. else if (Input.GetAxis("Mouse ScrollWheel") < 0 && !EventSystem.current.IsPointerOverGameObject())
  135. {
  136. targetPosition -= transform.forward * movementSpeed* mouseSpeed;
  137. Camera.main.orthographicSize++;
  138. }
  139. var marker_offset = new Vector3(target.position.x, targetPosition.y, target.position.z);
  140. targetPosition =Vector3.Lerp(transform.position, /*targetPosition + target.position - */marker_offset, (1.0f - smoothness));
  141. transform.position = targetPosition;
  142. if (Input.GetMouseButton(1))
  143. {
  144. axes = RotationAxes.Mouse;
  145. var player = PlayerController.instance;
  146. if (player.UserInfo.activeSelf)
  147. {
  148. var client = Client.instance;
  149. client.ImageStreamStopSend(player.user_broadcast.Value);
  150. player.user_broadcast = null;
  151. player.UserInfo.SetActive(false);
  152. }
  153. }
  154. }
  155. }