FreeCamera.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class FreeCamera : MonoBehaviour
  5. {
  6. [SerializeField] float MoveSpeed;
  7. [SerializeField] float MoveAccelerationSpeed;
  8. [SerializeField] float RotateSpeed;
  9. Vector3 Velocity;
  10. Vector3 MousePos;
  11. void Update()
  12. {
  13. Velocity.z = Mathf.MoveTowards (Velocity.z, Input.GetAxis ("Vertical") * MoveSpeed, MoveAccelerationSpeed * Time.deltaTime);
  14. Velocity.x = Mathf.MoveTowards (Velocity.x, Input.GetAxis ("Horizontal") * MoveSpeed, MoveAccelerationSpeed * Time.deltaTime);
  15. Velocity.y = Mathf.MoveTowards (Velocity.y, (Input.GetKey(KeyCode.E)? 1: Input.GetKey (KeyCode.Q)? -1: 0) * MoveSpeed * 0.3f, MoveAccelerationSpeed * Time.deltaTime);
  16. transform.position += transform.TransformDirection (Velocity);
  17. var mousePosDelta = Input.mousePosition - MousePos;
  18. MousePos = Input.mousePosition;
  19. transform.rotation *= Quaternion.AngleAxis (Input.GetAxis ("Mouse Y") * RotateSpeed, Vector3.left);
  20. transform.rotation *= Quaternion.AngleAxis (Input.GetAxis ("Mouse X") * RotateSpeed, Vector3.up);
  21. var eulerAngles = transform.eulerAngles;
  22. eulerAngles.z = 0;
  23. transform.eulerAngles = eulerAngles;
  24. if (Input.GetKey (KeyCode.LeftAlt))
  25. {
  26. Cursor.visible = true;
  27. Cursor.lockState = CursorLockMode.None;
  28. }
  29. else
  30. {
  31. Cursor.visible = false;
  32. Cursor.lockState = CursorLockMode.Locked;
  33. }
  34. Camera.main.fieldOfView = Mathf.Clamp (Camera.main.fieldOfView + Input.mouseScrollDelta.y, 5, 150);
  35. }
  36. }