SnapTurn.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (c) Valve Corporation, All rights reserved. ======================================================================================================
  2. // Modified by Lydia Ebbinghaus.
  3. // Deactivated Snaqp turn right.
  4. using UnityEngine;
  5. using System.Collections;
  6. namespace Valve.VR.InteractionSystem
  7. {
  8. //-----------------------------------------------------------------------------
  9. public class SnapTurn : MonoBehaviour
  10. {
  11. public float snapAngle = 90.0f;
  12. public bool showTurnAnimation = true;
  13. public AudioSource snapTurnSource;
  14. public AudioClip rotateSound;
  15. public GameObject rotateRightFX;
  16. public GameObject rotateLeftFX;
  17. public SteamVR_Action_Boolean snapLeftAction = SteamVR_Input.GetBooleanAction("SnapTurnLeft");
  18. public SteamVR_Action_Boolean snapRightAction = SteamVR_Input.GetBooleanAction("SnapTurnRight");
  19. public bool fadeScreen = true;
  20. public float fadeTime = 0.1f;
  21. public Color screenFadeColor = Color.black;
  22. public float distanceFromFace = 1.3f;
  23. public Vector3 additionalOffset = new Vector3(0, -0.3f, 0);
  24. public static float teleportLastActiveTime;
  25. private bool canRotate = true;
  26. public float canTurnEverySeconds = 0.4f;
  27. private void Start()
  28. {
  29. AllOff();
  30. }
  31. private void AllOff()
  32. {
  33. if (rotateLeftFX != null)
  34. rotateLeftFX.SetActive(false);
  35. if (rotateRightFX != null)
  36. rotateRightFX.SetActive(false);
  37. }
  38. private void Update()
  39. {
  40. Player player = Player.instance;
  41. if (canRotate && snapLeftAction != null && snapRightAction != null && snapLeftAction.activeBinding && snapRightAction.activeBinding)
  42. {
  43. //only allow snap turning after a quarter second after the last teleport
  44. if (Time.time < (teleportLastActiveTime + canTurnEverySeconds))
  45. return;
  46. // only allow snap turning when not holding something
  47. bool rightHandValid = player.rightHand.currentAttachedObject == null ||
  48. (player.rightHand.currentAttachedObject != null
  49. && player.rightHand.currentAttachedTeleportManager != null
  50. && player.rightHand.currentAttachedTeleportManager.teleportAllowed);
  51. bool leftHandValid = player.leftHand.currentAttachedObject == null ||
  52. (player.leftHand.currentAttachedObject != null
  53. && player.leftHand.currentAttachedTeleportManager != null
  54. && player.leftHand.currentAttachedTeleportManager.teleportAllowed);
  55. // Modified.
  56. bool leftHandTurnLeft = snapLeftAction.GetStateDown(SteamVR_Input_Sources.LeftHand) && leftHandValid;
  57. //bool rightHandTurnLeft = snapLeftAction.GetStateDown(SteamVR_Input_Sources.RightHand) && rightHandValid;
  58. bool rightHandTurnLeft = false;
  59. bool leftHandTurnRight = snapRightAction.GetStateDown(SteamVR_Input_Sources.LeftHand) && leftHandValid;
  60. //bool rightHandTurnRight = snapRightAction.GetStateDown(SteamVR_Input_Sources.RightHand) && rightHandValid;
  61. bool rightHandTurnRight = false;
  62. if (leftHandTurnLeft || rightHandTurnLeft)
  63. {
  64. RotatePlayer(-snapAngle);
  65. }
  66. else if (leftHandTurnRight || rightHandTurnRight)
  67. {
  68. RotatePlayer(snapAngle);
  69. }
  70. }
  71. }
  72. private Coroutine rotateCoroutine;
  73. public void RotatePlayer(float angle)
  74. {
  75. if (rotateCoroutine != null)
  76. {
  77. StopCoroutine(rotateCoroutine);
  78. AllOff();
  79. }
  80. rotateCoroutine = StartCoroutine(DoRotatePlayer(angle));
  81. }
  82. //-----------------------------------------------------
  83. private IEnumerator DoRotatePlayer(float angle)
  84. {
  85. Player player = Player.instance;
  86. canRotate = false;
  87. snapTurnSource.panStereo = angle / 90;
  88. snapTurnSource.PlayOneShot(rotateSound);
  89. if (fadeScreen)
  90. {
  91. SteamVR_Fade.Start(Color.clear, 0);
  92. Color tColor = screenFadeColor;
  93. tColor = tColor.linear * 0.6f;
  94. SteamVR_Fade.Start(tColor, fadeTime);
  95. }
  96. yield return new WaitForSeconds(fadeTime);
  97. Vector3 playerFeetOffset = player.trackingOriginTransform.position - player.feetPositionGuess;
  98. player.trackingOriginTransform.position -= playerFeetOffset;
  99. player.transform.Rotate(Vector3.up, angle);
  100. playerFeetOffset = Quaternion.Euler(0.0f, angle, 0.0f) * playerFeetOffset;
  101. player.trackingOriginTransform.position += playerFeetOffset;
  102. GameObject fx = angle > 0 ? rotateRightFX : rotateLeftFX;
  103. if (showTurnAnimation)
  104. ShowRotateFX(fx);
  105. if (fadeScreen)
  106. {
  107. SteamVR_Fade.Start(Color.clear, fadeTime);
  108. }
  109. float startTime = Time.time;
  110. float endTime = startTime + canTurnEverySeconds;
  111. while (Time.time <= endTime)
  112. {
  113. yield return null;
  114. UpdateOrientation(fx);
  115. };
  116. fx.SetActive(false);
  117. canRotate = true;
  118. }
  119. void ShowRotateFX(GameObject fx)
  120. {
  121. if (fx == null)
  122. return;
  123. fx.SetActive(false);
  124. UpdateOrientation(fx);
  125. fx.SetActive(true);
  126. UpdateOrientation(fx);
  127. }
  128. private void UpdateOrientation(GameObject fx)
  129. {
  130. Player player = Player.instance;
  131. //position fx in front of face
  132. this.transform.position = player.hmdTransform.position + (player.hmdTransform.forward * distanceFromFace);
  133. this.transform.rotation = Quaternion.LookRotation(player.hmdTransform.position - this.transform.position, Vector3.up);
  134. this.transform.Translate(additionalOffset, Space.Self);
  135. this.transform.rotation = Quaternion.LookRotation(player.hmdTransform.position - this.transform.position, Vector3.up);
  136. }
  137. }
  138. }