Remote.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Handle the Event in Defualt Mode
  3. *
  4. * Author: Jingyi Jia
  5. *
  6. */
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. using Valve.VR;
  12. using Valve.VR.InteractionSystem.Sample;
  13. using UnityEngine.UI;
  14. using UnityEngine.AI;
  15. public class Remote: MonoBehaviour,IMode
  16. {
  17. InteractionManagement interactionManagement;
  18. public Player_Control Player_Control;
  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. // The player can be raised or lowered (the aerial perspective is enabled)
  29. Player_Control.upDownEnabled = true;
  30. robot = GameObject.FindGameObjectWithTag("robot");
  31. navMeshAgent = robot.GetComponent<NavMeshAgent>();
  32. navMeshAgent.stoppingDistance = 1;
  33. interactionManagement = InteractionManagement.Instance;
  34. interactionManagement.SetPlayerText( "Control Mode : " + gameObject.name, 7, false);
  35. interactionManagement.SetMenu(false);
  36. // Add SteamVR Actions Listener
  37. SteamVR_Actions.default_Menu.AddOnStateDownListener(MenuActionHandler,SteamVR_Input_Sources.Any);
  38. SteamVR_Actions.default_Lock.AddOnStateDownListener(Locked_onStateDown,SteamVR_Input_Sources.Any);
  39. SteamVR_Actions.default_ModeMenu.AddOnStateDownListener(ModeMenuHandler,SteamVR_Input_Sources.Any);
  40. }
  41. // Start is called before the first frame update
  42. void Start()
  43. {
  44. }
  45. // Update is called once per frame
  46. void Update()
  47. {
  48. if(interactionManagement.Robot_Locked){
  49. signal = new Vector2(0,0);
  50. }
  51. else{
  52. UpdateSignal();
  53. }
  54. }
  55. public void UpdateSignal()
  56. {
  57. signal = Vector2.zero;
  58. // Auto driving
  59. if(targetLaser.autoDrive){
  60. navMeshAgent.SetDestination(targetLaser.targetPosition);
  61. float distance = Vector3.Distance(targetLaser.targetPosition, robot.transform.position);
  62. // Already arrive at the target position
  63. if(distance <= 1){
  64. stopAutoDrive();
  65. }
  66. }
  67. // get control signal from the joystick
  68. if(remoteController != null){
  69. if(remoteController.angle != 0 || remoteController.velocity !=0 ){
  70. signal = new Vector2(remoteController.angle,remoteController.velocity * 0.3f);
  71. if(targetLaser.autoDrive){
  72. stopAutoDrive();
  73. }
  74. }
  75. }
  76. }
  77. void stopAutoDrive(){
  78. signal = new Vector2(0,0);
  79. targetLaser.autoDrive = false;
  80. navMeshAgent.SetDestination(robot.transform.position);
  81. navMeshAgent.ResetPath();
  82. GameObject.Destroy(targetLaser.TargetFlag);
  83. //interactionManagement.SetPlayerText("Stop Auto Driving",5,false);
  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. // targetLaser.autoDrive = false;
  130. // GameObject.Destroy(targetLaser.TargetFlag);
  131. }
  132. }