Advertisement
Guest User

SimpleMouseRotator

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