AndroidMove.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // ***********************************************************
  2. // Written by Heyworks Unity Studio http://unity.heyworks.com/
  3. // ***********************************************************
  4. using UnityEngine;
  5. /// <summary>
  6. /// Gyroscope controller that works with any device orientation.
  7. /// </summary>
  8. public class AndroidMove : MonoBehaviour
  9. {
  10. #region [Private fields]
  11. private bool gyroEnabled = true;
  12. private const float lowPassFilterFactor = 0.2f;
  13. private readonly Quaternion baseIdentity = Quaternion.Euler(90, 0, 0);
  14. private readonly Quaternion landscapeRight = Quaternion.Euler(0, 0, 90);
  15. private readonly Quaternion landscapeLeft = Quaternion.Euler(0, 0, -90);
  16. private readonly Quaternion upsideDown = Quaternion.Euler(0, 0, 180);
  17. private Quaternion cameraBase = Quaternion.identity;
  18. private Quaternion calibration = Quaternion.identity;
  19. private Quaternion baseOrientation = Quaternion.Euler(90, 0, 0);
  20. private Quaternion baseOrientationRotationFix = Quaternion.identity;
  21. private Quaternion referanceRotation = Quaternion.identity;
  22. private bool debug = true;
  23. private GameObject RotateObjectGame;
  24. #endregion
  25. #region [Unity events]
  26. protected void Start()
  27. {
  28. // RotateObjectGame = gameObject;
  29. Input.gyro.enabled = true;
  30. AttachGyro();
  31. Invoke("GuiLing", 3f);
  32. }
  33. void GuiLing()
  34. {
  35. Debug.Log("this.transform.localEulerAngles.y"+ this.transform.localEulerAngles.y);
  36. this.transform.parent.transform.localEulerAngles = new Vector3(0, -this.transform.localEulerAngles.y, 0);
  37. }
  38. protected void Update()
  39. {
  40. if (!gyroEnabled)
  41. return;
  42. //transform.localRotation = Quaternion.Slerp(transform.localRotation,
  43. // cameraBase * (ConvertRotation(referanceRotation * Input.gyro.attitude) * GetRotFix()), lowPassFilterFactor);
  44. transform.localRotation = cameraBase * (ConvertRotation(referanceRotation * Input.gyro.attitude) * GetRotFix());
  45. //RotateObjectGame.transform.localEulerAngles = new Vector3 (0, 0, transform.localEulerAngles.y);
  46. }
  47. #endregion
  48. #region [Public methods]
  49. /// <summary>
  50. /// Attaches gyro controller to the transform.
  51. /// </summary>
  52. private void AttachGyro()
  53. {
  54. gyroEnabled = true;
  55. ResetBaseOrientation();
  56. UpdateCalibration(true);
  57. UpdateCameraBaseRotation(true);
  58. RecalculateReferenceRotation();
  59. }
  60. /// <summary>
  61. /// Detaches gyro controller from the transform
  62. /// </summary>
  63. private void DetachGyro()
  64. {
  65. gyroEnabled = false;
  66. }
  67. #endregion
  68. #region [Private methods]
  69. /// <summary>
  70. /// Update the gyro calibration.
  71. /// </summary>
  72. private void UpdateCalibration(bool onlyHorizontal)
  73. {
  74. if (onlyHorizontal)
  75. {
  76. var fw = (Input.gyro.attitude) * (-Vector3.forward);
  77. fw.z = 0;
  78. if (fw == Vector3.zero)
  79. {
  80. calibration = Quaternion.identity;
  81. }
  82. else
  83. {
  84. calibration = (Quaternion.FromToRotation(baseOrientationRotationFix * Vector3.up, fw));
  85. }
  86. }
  87. else
  88. {
  89. calibration = Input.gyro.attitude;
  90. }
  91. }
  92. /// <summary>
  93. /// Update the camera base localRotation.
  94. /// </summary>
  95. /// <param name='onlyHorizontal'>
  96. /// Only y localRotation.
  97. /// </param>
  98. private void UpdateCameraBaseRotation(bool onlyHorizontal)
  99. {
  100. if (onlyHorizontal)
  101. {
  102. var fw = transform.forward;
  103. fw.y = 0;
  104. if (fw == Vector3.zero)
  105. {
  106. cameraBase = Quaternion.identity;
  107. }
  108. else
  109. {
  110. cameraBase = Quaternion.FromToRotation(Vector3.forward, fw);
  111. }
  112. }
  113. else
  114. {
  115. cameraBase = transform.localRotation;
  116. }
  117. }
  118. /// <summary>
  119. /// Converts the localRotation from right handed to left handed.
  120. /// </summary>
  121. /// <returns>
  122. /// The result localRotation.
  123. /// </returns>
  124. /// <param name='q'>
  125. /// The localRotation to convert.
  126. /// </param>
  127. private static Quaternion ConvertRotation(Quaternion q)
  128. {
  129. return new Quaternion(q.x, q.y, -q.z, -q.w);
  130. }
  131. /// <summary>
  132. /// Gets the rot fix for different orientations.
  133. /// </summary>
  134. /// <returns>
  135. /// The rot fix.
  136. /// </returns>
  137. private Quaternion GetRotFix()
  138. {
  139. #if UNITY_3_5
  140. if (Screen.orientation == ScreenOrientation.Portrait)
  141. return Quaternion.identity;
  142. if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.Landscape)
  143. return landscapeLeft;
  144. if (Screen.orientation == ScreenOrientation.LandscapeRight)
  145. return landscapeRight;
  146. if (Screen.orientation == ScreenOrientation.PortraitUpsideDown)
  147. return upsideDown;
  148. return Quaternion.identity;
  149. #else
  150. return Quaternion.identity;
  151. #endif
  152. }
  153. /// <summary>
  154. /// Recalculates reference system.
  155. /// </summary>
  156. private void ResetBaseOrientation()
  157. {
  158. baseOrientationRotationFix = GetRotFix();
  159. baseOrientation = baseOrientationRotationFix * baseIdentity;
  160. }
  161. /// <summary>
  162. /// Recalculates reference localRotation.
  163. /// </summary>
  164. private void RecalculateReferenceRotation()
  165. {
  166. referanceRotation = Quaternion.Inverse(baseOrientation) * Quaternion.Inverse(calibration);
  167. }
  168. #endregion
  169. }