Remote.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Handle the Event in Remote Mode
  3. *
  4. */
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using Valve.VR;
  10. using Valve.VR.InteractionSystem.Sample;
  11. using UnityEngine.UI;
  12. using UnityEngine.AI;
  13. public class Remote: MonoBehaviour,IMode
  14. {
  15. InteractionManagement interactionManagement;
  16. public Player_Control Player_Control;
  17. // the remoteController will be set during running, which depends on the path of Remote GameObject
  18. // So be carefull to change the hierarchy of modes
  19. public RemoteController remoteController;
  20. public Valve.VR.InteractionSystem.TargetLaser targetLaser;
  21. public GameObject[] ModeMenu;
  22. public Vector2 signal;
  23. public Vector2 Signal => signal;
  24. SteamVR_Action_Vibration vibration = SteamVR_Input.GetVibrationAction("Haptic");
  25. GameObject robot;
  26. private NavMeshAgent navMeshAgent;
  27. private void OnEnable() {
  28. GameObject.Find("Input").GetComponent<VRInput>().SetMode(this.gameObject);
  29. // The player can be raised or lowered (the aerial perspective is enabled)
  30. Player_Control.upDownEnabled = true;
  31. robot = GameObject.FindGameObjectWithTag("robot");
  32. navMeshAgent = robot.GetComponent<NavMeshAgent>();
  33. navMeshAgent.stoppingDistance = 1;
  34. interactionManagement = InteractionManagement.Instance;
  35. interactionManagement.SetPlayerText( "Control Mode : " + gameObject.name, 7, false);
  36. interactionManagement.SetMenu(false);
  37. // Add SteamVR Actions Listener
  38. SteamVR_Actions.default_Menu.AddOnStateDownListener(MenuActionHandler,SteamVR_Input_Sources.Any);
  39. SteamVR_Actions.default_Lock.AddOnStateDownListener(Locked_onStateDown,SteamVR_Input_Sources.Any);
  40. SteamVR_Actions.default_ModeMenu.AddOnStateDownListener(ModeMenuHandler,SteamVR_Input_Sources.Any);
  41. }
  42. // Start is called before the first frame update
  43. void Start()
  44. {
  45. }
  46. // Update is called once per frame
  47. void Update()
  48. {
  49. if(interactionManagement.Robot_Locked){
  50. signal = new Vector2(0,0);
  51. }
  52. else{
  53. UpdateSignal();
  54. }
  55. }
  56. public void UpdateSignal()
  57. {
  58. signal = Vector2.zero;
  59. // Auto driving after the destination has been set
  60. if(targetLaser.autoDrive){
  61. navMeshAgent.SetDestination(targetLaser.targetPosition);
  62. float distance = Vector3.Distance(targetLaser.targetPosition, robot.transform.position);
  63. // Already arrive at the target position => stop auto driving
  64. if(distance <= 1){
  65. stopAutoDrive();
  66. }
  67. }
  68. // get control signal from the joystick
  69. if(remoteController != null){
  70. if(remoteController.angle != 0 || remoteController.velocity !=0 ){
  71. signal = new Vector2(remoteController.angle,remoteController.velocity);
  72. if(targetLaser.autoDrive){
  73. stopAutoDrive();
  74. }
  75. }
  76. }
  77. }
  78. void stopAutoDrive(){
  79. signal = new Vector2(0,0);
  80. targetLaser.autoDrive = false;
  81. navMeshAgent.SetDestination(robot.transform.position);
  82. navMeshAgent.ResetPath();
  83. GameObject.Destroy(targetLaser.TargetFlag);
  84. }
  85. private void MenuActionHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  86. {
  87. interactionManagement.SetMenu();
  88. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  89. if(interactionManagement.Menu_Opened){
  90. foreach(GameObject item in GameObject.FindGameObjectsWithTag("ItemPickup")){
  91. GameObject.Destroy(item);
  92. }
  93. stopAutoDrive();
  94. }
  95. }
  96. // Handle Robot Locking
  97. private void Locked_onStateDown(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  98. {
  99. if (!interactionManagement.Menu_Opened)
  100. {
  101. interactionManagement.LockRobot();
  102. }
  103. else {
  104. interactionManagement.SetPlayerText("Please close the menu first",5,false);
  105. }
  106. if(interactionManagement.Robot_Locked){
  107. stopAutoDrive();
  108. }
  109. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  110. }
  111. private void ModeMenuHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  112. {
  113. bool active = false;
  114. foreach(GameObject child in ModeMenu){
  115. active = !child.activeSelf;
  116. child.SetActive(!child.activeSelf);
  117. }
  118. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  119. }
  120. private void OnDisable() {
  121. // Remove SteamVR Actions Listener
  122. SteamVR_Actions.default_Menu.RemoveOnStateDownListener(MenuActionHandler,SteamVR_Input_Sources.Any);
  123. SteamVR_Actions.default_Lock.RemoveOnStateDownListener(Locked_onStateDown,SteamVR_Input_Sources.Any);
  124. SteamVR_Actions.default_ModeMenu.RemoveOnStateDownListener(ModeMenuHandler,SteamVR_Input_Sources.Any);
  125. foreach(GameObject item in GameObject.FindGameObjectsWithTag("ItemPickup")){
  126. GameObject.Destroy(item);
  127. }
  128. stopAutoDrive();
  129. }
  130. }