SimpleMouseLook.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // MIT License
  2. // https://gitlab.com/ilnprj
  3. // Copyright (c) 2020 ilnprj
  4. using UnityEngine;
  5. /// <summary>
  6. /// Simple Mouse Look script
  7. /// </summary>
  8. public class SimpleMouseLook : MonoBehaviour
  9. {
  10. [SerializeField]
  11. private bool isCursorLocked = false;
  12. [SerializeField]
  13. private float mouseSensitivity = 100.0f;
  14. [SerializeField]
  15. private float clampAngle = 80.0f;
  16. private float rotY = 0.0f;
  17. private float rotX = 0.0f;
  18. private void Start()
  19. {
  20. Vector3 rot = transform.localRotation.eulerAngles;
  21. rotY = rot.y;
  22. rotX = rot.x;
  23. Cursor.lockState = isCursorLocked ? CursorLockMode.Locked : CursorLockMode.None;
  24. }
  25. private void FixedUpdate()
  26. {
  27. float mouseX = Input.GetAxis("Mouse X");
  28. float mouseY = -Input.GetAxis("Mouse Y");
  29. rotY += mouseX * mouseSensitivity * Time.fixedDeltaTime;
  30. rotX += mouseY * mouseSensitivity * Time.fixedDeltaTime;
  31. rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
  32. Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
  33. transform.rotation = localRotation;
  34. }
  35. }