MouseLook.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using UnityEngine;
  3. using UnityStandardAssets.CrossPlatformInput;
  4. namespace UnityStandardAssets.Characters.FirstPerson
  5. {
  6. [Serializable]
  7. public class MouseLook
  8. {
  9. public float XSensitivity = 2f;
  10. public float YSensitivity = 2f;
  11. public bool clampVerticalRotation = true;
  12. public float MinimumX = -90F;
  13. public float MaximumX = 90F;
  14. public bool smooth;
  15. public float smoothTime = 5f;
  16. public bool lockCursor = true;
  17. private Quaternion m_CharacterTargetRot;
  18. private Quaternion m_CameraTargetRot;
  19. private bool m_cursorIsLocked = true;
  20. public void Init(Transform character, Transform camera)
  21. {
  22. m_CharacterTargetRot = character.localRotation;
  23. m_CameraTargetRot = camera.localRotation;
  24. }
  25. public void LookRotation(Transform character, Transform camera)
  26. {
  27. float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
  28. float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;
  29. m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
  30. m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
  31. if(clampVerticalRotation)
  32. m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);
  33. if(smooth)
  34. {
  35. character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
  36. smoothTime * Time.deltaTime);
  37. camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
  38. smoothTime * Time.deltaTime);
  39. }
  40. else
  41. {
  42. character.localRotation = m_CharacterTargetRot;
  43. camera.localRotation = m_CameraTargetRot;
  44. }
  45. UpdateCursorLock();
  46. }
  47. public void SetCursorLock(bool value)
  48. {
  49. lockCursor = value;
  50. if(!lockCursor)
  51. {//we force unlock the cursor if the user disable the cursor locking helper
  52. Cursor.lockState = CursorLockMode.None;
  53. Cursor.visible = true;
  54. }
  55. }
  56. public void UpdateCursorLock()
  57. {
  58. //if the user set "lockCursor" we check & properly lock the cursos
  59. if (lockCursor)
  60. InternalLockUpdate();
  61. }
  62. private void InternalLockUpdate()
  63. {
  64. if(Input.GetKeyUp(KeyCode.Escape))
  65. {
  66. m_cursorIsLocked = false;
  67. }
  68. else if(Input.GetMouseButtonUp(0))
  69. {
  70. m_cursorIsLocked = true;
  71. }
  72. if (m_cursorIsLocked)
  73. {
  74. Cursor.lockState = CursorLockMode.Locked;
  75. Cursor.visible = false;
  76. }
  77. else if (!m_cursorIsLocked)
  78. {
  79. Cursor.lockState = CursorLockMode.None;
  80. Cursor.visible = true;
  81. }
  82. }
  83. Quaternion ClampRotationAroundXAxis(Quaternion q)
  84. {
  85. q.x /= q.w;
  86. q.y /= q.w;
  87. q.z /= q.w;
  88. q.w = 1.0f;
  89. float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
  90. angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
  91. q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
  92. return q;
  93. }
  94. }
  95. }