MoveCamera.cs 3.5 KB

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