MouseLook.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace HQFPSWeapons
  4. {
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. public class MouseLook : PlayerComponent
  9. {
  10. public float SensitivityFactor { get; set; }
  11. public Vector2 LookAngles
  12. {
  13. get => m_LookAngles;
  14. set
  15. {
  16. m_LookAngles = value;
  17. }
  18. }
  19. public Vector2 LastMovement { get; private set; }
  20. [BHeader("General", true)]
  21. [SerializeField]
  22. [Tooltip("The camera root which will be rotated up & down (on the X axis).")]
  23. private Transform m_LookRoot = null;
  24. [SerializeField]
  25. private Transform m_PlayerRoot = null;
  26. [SerializeField]
  27. [Tooltip("The up & down rotation will be inverted, if checked.")]
  28. private bool m_Invert = false;
  29. [BHeader("Motion")]
  30. [SerializeField]
  31. [Tooltip("The higher it is, the faster the camera will rotate.")]
  32. private float m_Sensitivity = 5f;
  33. [SerializeField]
  34. private float m_AimSensitivity = 2.5f;
  35. [SerializeField]
  36. private float m_RollAngle = 10f;
  37. [SerializeField]
  38. private float m_RollSpeed = 3f;
  39. [BHeader("Rotation Limits")]
  40. [SerializeField]
  41. private Vector2 m_DefaultLookLimits = new Vector2(-60f, 90f);
  42. private float m_CurrentRollAngle;
  43. private Vector2 m_LookAngles;
  44. private bool m_Loaded;
  45. public void MoveCamera(float verticalMove, float horizontalMove)
  46. {
  47. LookAngles += new Vector2(verticalMove, horizontalMove);
  48. }
  49. public void OnLoad()
  50. {
  51. m_Loaded = true;
  52. }
  53. private void Awake()
  54. {
  55. SensitivityFactor = 1f;
  56. }
  57. private void Start()
  58. {
  59. if(!m_LookRoot)
  60. {
  61. Debug.LogErrorFormat(this, "Assign the look root in the inspector!", name);
  62. enabled = false;
  63. }
  64. Cursor.visible = false;
  65. Cursor.lockState = CursorLockMode.Locked;
  66. if(!m_Loaded)
  67. m_LookAngles = new Vector2(transform.localEulerAngles.x, m_PlayerRoot.localEulerAngles.y);
  68. }
  69. private void LateUpdate()
  70. {
  71. Vector2 prevLookAngles = m_LookAngles;
  72. if (Player.ViewLocked.Is(false) && Player.Health.Get() > 0f)
  73. {
  74. LookAround();
  75. }
  76. LastMovement = m_LookAngles - prevLookAngles;
  77. }
  78. /// <summary>
  79. /// Rotates the camera and character and creates a sensation of looking around.
  80. /// </summary>
  81. private void LookAround()
  82. {
  83. var sensitivity = Player.Aim.Active ? m_AimSensitivity : m_Sensitivity;
  84. sensitivity *= SensitivityFactor;
  85. m_LookAngles.x += Player.LookInput.Get().y * sensitivity * (m_Invert ? 1f : -1f);
  86. m_LookAngles.y += Player.LookInput.Get().x * sensitivity;
  87. m_LookAngles.x = ClampAngle(m_LookAngles.x, m_DefaultLookLimits.x, m_DefaultLookLimits.y);
  88. m_CurrentRollAngle = Mathf.Lerp(m_CurrentRollAngle, Player.LookInput.Get().x * m_RollAngle, Time.deltaTime * m_RollSpeed);
  89. // Apply the current up & down rotation to the look root.
  90. m_LookRoot.localRotation = Quaternion.Euler(m_LookAngles.x, 0f, 0f);
  91. m_PlayerRoot.localRotation = Quaternion.Euler(0f, m_LookAngles.y, 0f);
  92. Entity.LookDirection.Set(m_LookRoot.forward);
  93. }
  94. /// <summary>
  95. /// Clamps the given angle between min and max degrees.
  96. /// </summary>
  97. private float ClampAngle(float angle, float min, float max)
  98. {
  99. if(angle > 360f)
  100. angle -= 360f;
  101. else if(angle < -360f)
  102. angle += 360f;
  103. return Mathf.Clamp(angle, min, max);
  104. }
  105. }
  106. }