CameraMovement.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using UnityEngine;
  2. namespace Kamgam.SkyClouds
  3. {
  4. /// <summary>
  5. /// Editor like camera controls for in-game camera.<br />
  6. /// Thanks to the group effort in the forum, see:
  7. /// https://forum.unity.com/threads/how-to-make-camera-move-in-a-way-similar-to-editor-scene.524645/#post-8302236
  8. /// </summary>
  9. using UnityEngine;
  10. public class CameraMovement : MonoBehaviour
  11. {
  12. [SerializeField] float navigationSpeed = 2f;
  13. [SerializeField] float shiftMultiplier = 2f;
  14. [SerializeField] float sensitivity = 0.15f;
  15. [SerializeField] float panSensitivity = 0.5f;
  16. [SerializeField] float mouseWheelZoomSpeed = 5f;
  17. [SerializeField] bool usePhysics = false;
  18. private Camera cam;
  19. private Vector3 anchorPoint;
  20. private Quaternion anchorRot;
  21. private bool isPanning;
  22. private Vector3 move;
  23. private Quaternion rotation;
  24. protected Rigidbody _rigidbody;
  25. public Rigidbody Rigidbody
  26. {
  27. get
  28. {
  29. if (_rigidbody == null)
  30. {
  31. _rigidbody = this.GetComponent<Rigidbody>();
  32. }
  33. return _rigidbody;
  34. }
  35. }
  36. private bool shouldUsePhysics => usePhysics && Rigidbody != null;
  37. private void Awake()
  38. {
  39. cam = GetComponent<Camera>();
  40. move = Vector3.zero;
  41. rotation = transform.rotation;
  42. }
  43. void Update()
  44. {
  45. move = Vector3.zero;
  46. rotation = transform.rotation;
  47. MousePanning();
  48. if (isPanning)
  49. { return; }
  50. if (Input.GetMouseButton(1))
  51. {
  52. float speed = navigationSpeed * (Input.GetKey(KeyCode.LeftShift) ? shiftMultiplier : 1f) * 9.1f;
  53. if (Input.GetKey(KeyCode.W))
  54. move += Vector3.forward * speed;
  55. if (Input.GetKey(KeyCode.S))
  56. move -= Vector3.forward * speed;
  57. if (Input.GetKey(KeyCode.D))
  58. move += Vector3.right * speed;
  59. if (Input.GetKey(KeyCode.A))
  60. move -= Vector3.right * speed;
  61. if (Input.GetKey(KeyCode.E))
  62. move += Vector3.up * speed;
  63. if (Input.GetKey(KeyCode.Q))
  64. move -= Vector3.up * speed;
  65. if (!shouldUsePhysics)
  66. {
  67. transform.Translate(move * Time.deltaTime);
  68. }
  69. }
  70. if (Input.GetMouseButtonDown(1))
  71. {
  72. anchorPoint = new Vector3(Input.mousePosition.y, -Input.mousePosition.x);
  73. anchorRot = transform.rotation;
  74. }
  75. if (Input.GetMouseButton(1))
  76. {
  77. Quaternion rot = anchorRot;
  78. Vector3 dif = anchorPoint - new Vector3(Input.mousePosition.y, -Input.mousePosition.x);
  79. rot.eulerAngles += dif * sensitivity;
  80. if (!shouldUsePhysics)
  81. transform.rotation = rot;
  82. else
  83. rotation = rot;
  84. }
  85. MouseWheeling();
  86. }
  87. private void FixedUpdate()
  88. {
  89. if (shouldUsePhysics)
  90. {
  91. Rigidbody.MovePosition(transform.position + transform.TransformVector(move * Time.fixedDeltaTime));
  92. Rigidbody.MoveRotation(rotation);
  93. Rigidbody.freezeRotation = true;
  94. Rigidbody.angularVelocity = Vector3.zero;
  95. #if UNITY_6000_0_OR_NEWER
  96. Rigidbody.linearVelocity = Vector3.zero;
  97. #else
  98. Rigidbody.velocity = Vector3.zero;
  99. #endif
  100. }
  101. }
  102. //Zoom with mouse wheel
  103. void MouseWheeling()
  104. {
  105. float speed = 10 * (mouseWheelZoomSpeed * (Input.GetKey(KeyCode.LeftShift) ? shiftMultiplier : 1f) * Time.deltaTime * 9.1f);
  106. Vector3 pos = transform.position;
  107. if (Input.GetAxis("Mouse ScrollWheel") < 0)
  108. {
  109. pos = pos - (transform.forward * speed);
  110. if (!shouldUsePhysics)
  111. transform.position = pos;
  112. else
  113. move = pos - transform.position;
  114. }
  115. if (Input.GetAxis("Mouse ScrollWheel") > 0)
  116. {
  117. pos = pos + (transform.forward * speed);
  118. if (!shouldUsePhysics)
  119. transform.position = pos;
  120. else
  121. move = pos - transform.position;
  122. }
  123. }
  124. private float pan_x;
  125. private float pan_y;
  126. private Vector3 panComplete;
  127. void MousePanning()
  128. {
  129. pan_x = -Input.GetAxis("Mouse X") * panSensitivity;
  130. pan_y = -Input.GetAxis("Mouse Y") * panSensitivity;
  131. panComplete = new Vector3(pan_x, pan_y, 0);
  132. if (Input.GetMouseButtonDown(2))
  133. {
  134. isPanning = true;
  135. }
  136. if (Input.GetMouseButtonUp(2))
  137. {
  138. isPanning = false;
  139. }
  140. if (isPanning)
  141. {
  142. if (!shouldUsePhysics)
  143. transform.Translate(panComplete);
  144. else
  145. move = panComplete;
  146. }
  147. }
  148. }
  149. }