CameraControllerFPS.cs 975 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion {
  4. /// <summary>
  5. /// The very simple FPS camera.
  6. /// </summary>
  7. public class CameraControllerFPS: MonoBehaviour {
  8. public float rotationSensitivity = 3f;
  9. public float yMinLimit = -89f;
  10. public float yMaxLimit = 89f;
  11. private float x, y;
  12. void Awake () {
  13. Vector3 angles = transform.eulerAngles;
  14. x = angles.y;
  15. y = angles.x;
  16. }
  17. public void LateUpdate() {
  18. Cursor.lockState = CursorLockMode.Locked;
  19. x += Input.GetAxis("Mouse X") * rotationSensitivity;
  20. y = ClampAngle(y - Input.GetAxis("Mouse Y") * rotationSensitivity, yMinLimit, yMaxLimit);
  21. // Rotation
  22. transform.rotation = Quaternion.AngleAxis(x, Vector3.up) * Quaternion.AngleAxis(y, Vector3.right);
  23. }
  24. // Clamping Euler angles
  25. private float ClampAngle (float angle, float min, float max) {
  26. if (angle < -360) angle += 360;
  27. if (angle > 360) angle -= 360;
  28. return Mathf.Clamp (angle, min, max);
  29. }
  30. }
  31. }