MoveCamera.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. private void Start()
  20. {
  21. cam = GameObject.Find("MoveCamera").GetComponent<Camera>();
  22. }
  23. void Update()
  24. {
  25. // Mouse camera angle
  26. if (Input.GetMouseButton(0))
  27. transform.eulerAngles += rotationSpeed * new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);
  28. // MouseWheel Zoom In Out
  29. float fov = cam.fieldOfView;
  30. fov -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
  31. fov = Mathf.Clamp(fov, minFov, maxFov);
  32. cam.fieldOfView = fov;
  33. // Keyboard commands camera movement
  34. Vector3 p = GetBaseInput();
  35. if (p.sqrMagnitude > 0)
  36. {
  37. // only move while a direction key is pressed
  38. if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  39. {
  40. totalRun += Time.deltaTime;
  41. p = p * totalRun * shiftAdd;
  42. p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
  43. p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
  44. p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
  45. }
  46. else
  47. {
  48. totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
  49. p = p * mainSpeed;
  50. }
  51. p = p * Time.deltaTime;
  52. Vector3 newPosition = transform.position;
  53. if (Input.GetKey(KeyCode.Space))
  54. {
  55. transform.Translate(p);
  56. }
  57. else
  58. {
  59. //If player wants to move on X and Z axis only
  60. transform.Translate(p);
  61. newPosition.x = transform.position.x;
  62. newPosition.z = transform.position.z;
  63. transform.position = newPosition;
  64. }
  65. }
  66. }
  67. private Vector3 GetBaseInput()
  68. { //returns the basic values, if it's 0 than it's not active.
  69. Vector3 p_Velocity = new Vector3();
  70. if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
  71. {
  72. p_Velocity += new Vector3(0, 0, 1);
  73. }
  74. if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
  75. {
  76. p_Velocity += new Vector3(0, 0, -1);
  77. }
  78. if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
  79. {
  80. p_Velocity += new Vector3(-1, 0, 0);
  81. }
  82. if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
  83. {
  84. p_Velocity += new Vector3(1, 0, 0);
  85. }
  86. return p_Velocity;
  87. }
  88. }