Player_Control.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Player_Control.cs
  2. * author: Yannic Seidler
  3. * modified: Jingyi deleted TeleportingGround controls, change speed
  4. */
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using Valve.VR.InteractionSystem;
  10. using Valve.VR;
  11. // Handles TeleportingGround controls
  12. // Attached to TeleportingGround
  13. public class Player_Control : MonoBehaviour
  14. {
  15. public bool upDownEnabled = true;
  16. public SteamVR_ActionSet defaultActionSet;
  17. public SteamVR_ActionSet playerControlActionSet;
  18. public SteamVR_Action_Boolean button_down;
  19. public SteamVR_Action_Boolean button_up;
  20. // continous motion if true.
  21. public bool smooth_Y;
  22. public float up_down_speed;
  23. public double percentageOfRadius;
  24. Player player;
  25. const double standardSize = 5.0;
  26. double maxRadius;
  27. double usedRadius;
  28. // Start is called before the first frame update.
  29. void Start()
  30. {
  31. InitializeButtons();
  32. InitializeTeleportRadius(percentageOfRadius);
  33. Vector3 initPosition = player.feetPositionGuess;
  34. initPosition.y=0;
  35. transform.position = initPosition;
  36. }
  37. // Calculate the max radius a player is able to move in. If the radius is exceeded, center the plane again with.
  38. private void InitializeTeleportRadius(double percentageOfRadius)
  39. {
  40. player = Player.instance;
  41. double r1 = standardSize * (double)transform.localScale.x;
  42. double r2 = standardSize * (double)transform.localScale.z;
  43. maxRadius = (double)Mathf.Min((float)r1, (float)r2);
  44. if (0 < percentageOfRadius && percentageOfRadius < 1)
  45. {
  46. usedRadius = (double)maxRadius * percentageOfRadius;
  47. }
  48. else
  49. {
  50. usedRadius = maxRadius;
  51. }
  52. }
  53. // Initialize button actions for y position controls of player.
  54. private void InitializeButtons()
  55. {
  56. button_down.onStateDown += Button_down_onStateDown;
  57. button_down.onState += Button_down_onState;
  58. button_down.onStateUp += Button_down_onStateUp;
  59. button_up.onStateDown += Button_up_onStateDown;
  60. button_up.onState += Button_up_onState;
  61. button_up.onStateUp += Button_up_onStateUp;
  62. playerControlActionSet.Activate();
  63. }
  64. private void Button_up_onStateUp(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  65. {
  66. defaultActionSet.Activate();
  67. }
  68. private void Button_down_onStateUp(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  69. {
  70. defaultActionSet.Activate();
  71. }
  72. private void Button_down_onState(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  73. {
  74. if (smooth_Y && (player.feetPositionGuess.y)>0 && upDownEnabled)
  75. {
  76. defaultActionSet.Deactivate();
  77. //transform.Translate(Vector3.down * up_down_speed * Time.deltaTime);
  78. player.trackingOriginTransform.Translate(Vector3.down * up_down_speed * Time.deltaTime);
  79. }
  80. }
  81. private void Button_down_onStateDown(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  82. {
  83. if (!smooth_Y && (player.feetPositionGuess.y)>0 && upDownEnabled)
  84. {
  85. defaultActionSet.Deactivate();
  86. //transform.Translate(Vector3.down * 2 * up_down_speed * Time.deltaTime);
  87. player.trackingOriginTransform.Translate(Vector3.down * 2 * up_down_speed * Time.deltaTime);
  88. }
  89. }
  90. private void Button_up_onState(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  91. {
  92. if (smooth_Y && upDownEnabled)
  93. {
  94. defaultActionSet.Deactivate();
  95. player.trackingOriginTransform.Translate(Vector3.up * up_down_speed * Time.deltaTime);
  96. //transform.Translate(Vector3.up * up_down_speed * Time.deltaTime);
  97. }
  98. }
  99. private void Button_up_onStateDown(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  100. {
  101. if (!smooth_Y && upDownEnabled)
  102. {
  103. defaultActionSet.Deactivate();
  104. player.trackingOriginTransform.Translate(Vector3.up * 2 * up_down_speed * Time.deltaTime);
  105. //transform.Translate(Vector3.up * up_down_speed * 2 * Time.deltaTime);
  106. }
  107. }
  108. // Recenter plane
  109. void Update()
  110. {
  111. //transform.position = player.feetPositionGuess;
  112. /*
  113. var ray = new Ray(player.hmdTransform.position, Vector3.down);
  114. RaycastHit hit;
  115. if (Physics.Raycast(ray, out hit))
  116. {
  117. if (gameObject.Equals(hit.transform.gameObject))
  118. {
  119. if ((double)Mathf.Abs(Vector3.Distance(transform.position, hit.point)) > usedRadius)
  120. {
  121. Vector3 move = hit.point - transform.position;
  122. move.y = 0;
  123. transform.Translate(move);
  124. }
  125. }
  126. }
  127. else
  128. {
  129. Debug.Log("Player_Control.cs: Nothing hit. May occur if percentageOfRadius = 1");
  130. Vector3 move =player.feetPositionGuess - transform.position;
  131. move.y = 0;
  132. transform.Translate(move);
  133. }
  134. */
  135. Vector3 move = player.feetPositionGuess - transform.position;
  136. move.y = 0;
  137. transform.Translate(move);
  138. }
  139. void OnDestroy()
  140. {
  141. button_down.onStateDown -= Button_down_onStateDown;
  142. button_down.onState -= Button_down_onState;
  143. button_down.onStateUp -= Button_down_onStateUp;
  144. button_up.onStateDown -= Button_up_onStateDown;
  145. button_up.onState -= Button_up_onState;
  146. button_up.onStateUp -= Button_up_onStateUp;
  147. playerControlActionSet.Deactivate();
  148. }
  149. }