You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
4.4 KiB

  1. using System;
  2. using UnityEngine;
  3. using UnityStandardAssets.CrossPlatformInput;
  4. namespace UnityStandardAssets.Utility
  5. {
  6. public class SimpleMouseRotator : MonoBehaviour
  7. {
  8. // A mouselook behaviour with constraints which operate relative to
  9. // this gameobject's initial rotation.
  10. // Only rotates around local X and Y.
  11. // Works in local coordinates, so if this object is parented
  12. // to another moving gameobject, its local constraints will
  13. // operate correctly
  14. // (Think: looking out the side window of a car, or a gun turret
  15. // on a moving spaceship with a limited angular range)
  16. // to have no constraints on an axis, set the rotationRange to 360 or greater.
  17. public Vector2 rotationRange = new Vector3(70, 70);
  18. public float rotationSpeed = 10;
  19. public float dampingTime = 0.2f;
  20. public bool autoZeroVerticalOnMobile = true;
  21. public bool autoZeroHorizontalOnMobile = false;
  22. public bool relative = true;
  23. private Vector3 m_TargetAngles;
  24. private Vector3 m_FollowAngles;
  25. private Vector3 m_FollowVelocity;
  26. private Quaternion m_OriginalRotation;
  27. private void Start()
  28. {
  29. m_OriginalRotation = transform.localRotation;
  30. }
  31. private void Update()
  32. {
  33. // we make initial calculations from the original local rotation
  34. transform.localRotation = m_OriginalRotation;
  35. // read input from mouse or mobile controls
  36. float inputH;
  37. float inputV;
  38. if (relative)
  39. {
  40. inputH = CrossPlatformInputManager.GetAxis("Mouse X");
  41. inputV = CrossPlatformInputManager.GetAxis("Mouse Y");
  42. // wrap values to avoid springing quickly the wrong way from positive to negative
  43. if (m_TargetAngles.y > 180)
  44. {
  45. m_TargetAngles.y -= 360;
  46. m_FollowAngles.y -= 360;
  47. }
  48. if (m_TargetAngles.x > 180)
  49. {
  50. m_TargetAngles.x -= 360;
  51. m_FollowAngles.x -= 360;
  52. }
  53. if (m_TargetAngles.y < -180)
  54. {
  55. m_TargetAngles.y += 360;
  56. m_FollowAngles.y += 360;
  57. }
  58. if (m_TargetAngles.x < -180)
  59. {
  60. m_TargetAngles.x += 360;
  61. m_FollowAngles.x += 360;
  62. }
  63. #if MOBILE_INPUT
  64. // on mobile, sometimes we want input mapped directly to tilt value,
  65. // so it springs back automatically when the look input is released.
  66. if (autoZeroHorizontalOnMobile) {
  67. m_TargetAngles.y = Mathf.Lerp (-rotationRange.y * 0.5f, rotationRange.y * 0.5f, inputH * .5f + .5f);
  68. } else {
  69. m_TargetAngles.y += inputH * rotationSpeed;
  70. }
  71. if (autoZeroVerticalOnMobile) {
  72. m_TargetAngles.x = Mathf.Lerp (-rotationRange.x * 0.5f, rotationRange.x * 0.5f, inputV * .5f + .5f);
  73. } else {
  74. m_TargetAngles.x += inputV * rotationSpeed;
  75. }
  76. #else
  77. // with mouse input, we have direct control with no springback required.
  78. m_TargetAngles.y += inputH*rotationSpeed;
  79. m_TargetAngles.x += inputV*rotationSpeed;
  80. #endif
  81. // clamp values to allowed range
  82. m_TargetAngles.y = Mathf.Clamp(m_TargetAngles.y, -rotationRange.y*0.5f, rotationRange.y*0.5f);
  83. m_TargetAngles.x = Mathf.Clamp(m_TargetAngles.x, -rotationRange.x*0.5f, rotationRange.x*0.5f);
  84. }
  85. else
  86. {
  87. inputH = Input.mousePosition.x;
  88. inputV = Input.mousePosition.y;
  89. // set values to allowed range
  90. m_TargetAngles.y = Mathf.Lerp(-rotationRange.y*0.5f, rotationRange.y*0.5f, inputH/Screen.width);
  91. m_TargetAngles.x = Mathf.Lerp(-rotationRange.x*0.5f, rotationRange.x*0.5f, inputV/Screen.height);
  92. }
  93. // smoothly interpolate current values to target angles
  94. m_FollowAngles = Vector3.SmoothDamp(m_FollowAngles, m_TargetAngles, ref m_FollowVelocity, dampingTime);
  95. // update the actual gameobject's rotation
  96. transform.localRotation = m_OriginalRotation*Quaternion.Euler(-m_FollowAngles.x, m_FollowAngles.y, 0);
  97. }
  98. }
  99. }