MoveCamera.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. public class MoveCamera : MonoBehaviour
  4. {
  5. private Camera cam;
  6. // Rotation variables
  7. [SerializeField]
  8. private float rotationSpeed = 5f; //How sensitive it with mouse
  9. [SerializeField]
  10. private float maxVerticalRotation = 45.0f;
  11. private float rotationX = 0.0f;
  12. private float rotationY = 0.0f;
  13. // Zoom variables
  14. //[SerializeField]
  15. //private float zoomSpeed = 20f; //How sensitive it with mousewheel
  16. //private float minFov = 10f;
  17. //private float maxFov = 90f;
  18. // Scroll variables (Go up, down in World coordinates)
  19. [SerializeField]
  20. private float scrollSpeed = 20f;
  21. // Move variables
  22. [SerializeField]
  23. private float mainSpeed = 7.0f; //regular speed
  24. private float shiftAdd = 5.0f; //multiplied by how long shift is held. Basically running
  25. private float maxShift = 27.0f; //Maximum speed when holding shift
  26. private float totalRun = 1.0f;
  27. // Reset variables
  28. private Vector3 startPos;
  29. private Quaternion startRot;
  30. private void Start()
  31. {
  32. cam = GameObject.Find("MoveCamera").GetComponent<Camera>();
  33. startPos = cam.transform.position;
  34. startRot = cam.transform.rotation;
  35. rotationX = transform.eulerAngles.x;
  36. rotationY = transform.eulerAngles.y;
  37. }
  38. void Update()
  39. {
  40. if (Input.GetKey(KeyCode.R) || Input.GetKey(KeyCode.Escape))
  41. {
  42. cam.transform.position = startPos;
  43. cam.transform.rotation = startRot;
  44. }
  45. // Mouse camera angle
  46. if (Input.GetMouseButton(0))
  47. {
  48. // Blocks rotating camera if the playback is used
  49. if (EventSystem.current.currentSelectedGameObject != null)
  50. return;
  51. // Clamps the vertical rotation to maxVrticalRotation
  52. rotationY += rotationSpeed * Input.GetAxis("Mouse X");
  53. rotationX -= rotationSpeed * Input.GetAxis("Mouse Y");
  54. rotationX = Mathf.Clamp(rotationX, -maxVerticalRotation, maxVerticalRotation);
  55. transform.eulerAngles = new Vector3(rotationX, rotationY, 0);
  56. }
  57. // MouseWheel Zoom In Out
  58. //float fov = cam.fieldOfView;
  59. //fov -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
  60. //fov = Mathf.Clamp(fov, minFov, maxFov);
  61. //cam.fieldOfView = fov;
  62. // MouseWheel up and down
  63. float scroll = Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
  64. transform.Translate(new Vector3(0, scroll, 0), Space.World);
  65. // Keyboard commands camera movement
  66. Vector3 p = GetBaseInput();
  67. if (p.sqrMagnitude > 0)
  68. {
  69. // only move while a direction key is pressed
  70. if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  71. {
  72. totalRun += Time.deltaTime;
  73. p = p * totalRun * shiftAdd;
  74. p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
  75. p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
  76. p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
  77. }
  78. else
  79. {
  80. totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
  81. p = p * mainSpeed;
  82. }
  83. p = p * Time.deltaTime;
  84. Vector3 newPosition = transform.position;
  85. if (Input.GetKey(KeyCode.Space))
  86. {
  87. transform.Translate(p);
  88. }
  89. else
  90. {
  91. //If player wants to move on X and Z axis only
  92. transform.Translate(p);
  93. newPosition.x = transform.position.x;
  94. newPosition.z = transform.position.z;
  95. transform.position = newPosition;
  96. }
  97. }
  98. }
  99. private Vector3 GetBaseInput()
  100. { //returns the basic values, if it's 0 than it's not active.
  101. Vector3 p_Velocity = new Vector3();
  102. if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
  103. {
  104. p_Velocity += new Vector3(0, 0, 1);
  105. }
  106. if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
  107. {
  108. p_Velocity += new Vector3(0, 0, -1);
  109. }
  110. if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
  111. {
  112. p_Velocity += new Vector3(-1, 0, 0);
  113. }
  114. if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
  115. {
  116. p_Velocity += new Vector3(1, 0, 0);
  117. }
  118. return p_Velocity;
  119. }
  120. }