FreeCam.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using UnityEngine;
  2. public class FreeCam : MonoBehaviour
  3. {
  4. public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
  5. public RotationAxes axes = RotationAxes.MouseXAndY;
  6. public float sensitivityX = 15F;
  7. public float sensitivityY = 15F;
  8. public float minimumX = -360F;
  9. public float maximumX = 360F;
  10. public float minimumY = -60F;
  11. public float maximumY = 60F;
  12. public float moveSpeed = 1.0f;
  13. public bool lockHeight = false;
  14. float rotationY = 0F;
  15. void Update()
  16. {
  17. if (axes == RotationAxes.MouseXAndY)
  18. {
  19. float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
  20. rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  21. rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
  22. transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
  23. }
  24. else if (axes == RotationAxes.MouseX)
  25. {
  26. transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
  27. }
  28. else
  29. {
  30. rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  31. rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
  32. transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
  33. }
  34. var xAxisValue = Input.GetAxis("Horizontal");
  35. var zAxisValue = Input.GetAxis("Vertical");
  36. if (lockHeight)
  37. {
  38. var dir = transform.TransformDirection(new Vector3(xAxisValue, 0.0f, zAxisValue) * moveSpeed);
  39. dir.y = 0.0f;
  40. transform.position += dir;
  41. }
  42. else
  43. {
  44. transform.Translate(new Vector3(xAxisValue, 0.0f, zAxisValue) * moveSpeed);
  45. }
  46. }
  47. }