UI_Mode.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Handle the Event in UI Mode
  3. *
  4. *
  5. */
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using Valve.VR;
  11. using Valve.VR.InteractionSystem.Sample;
  12. using UnityEngine.UI;
  13. using Valve.VR.InteractionSystem;
  14. using UnityEngine.AI;
  15. public class UI_Mode: MonoBehaviour,IMode
  16. {
  17. // ------------------------ UI Menu
  18. public UnityEngine.UI.Toggle lock_robot_Toggle;
  19. public UnityEngine.UI.Toggle follow_me_Toggle;
  20. public UnityEngine.UI.Slider velocity_Slider;
  21. public bool userControl = false;
  22. // ------------------------
  23. InteractionManagement interactionManagement;
  24. public Player_Control Player_Control;
  25. public GameObject[] ModeMenu;
  26. public Vector2 signal;
  27. public Vector2 Signal => signal;
  28. SteamVR_Action_Vibration vibration = SteamVR_Input.GetVibrationAction("Haptic");
  29. GameObject robot;
  30. Player player;
  31. private NavMeshAgent navMeshAgent;
  32. private float originSpeed; // origin speed from navMEshAgent;
  33. private void OnEnable() {
  34. GameObject.Find("Input").GetComponent<VRInput>().SetMode(this.gameObject);
  35. // The player can be raised or lowered (the aerial perspective is enabled)
  36. Player_Control.upDownEnabled = true;
  37. robot = GameObject.FindGameObjectWithTag("robot");
  38. navMeshAgent = robot.GetComponent<NavMeshAgent>();
  39. originSpeed = navMeshAgent.speed;
  40. navMeshAgent.stoppingDistance = 2;
  41. player = Player.instance;
  42. interactionManagement = InteractionManagement.Instance;
  43. interactionManagement.SetPlayerText( "Control Mode : " + gameObject.name, 7, false);
  44. interactionManagement.SetMenu(false);
  45. // open the mode menu
  46. foreach(GameObject child in ModeMenu){
  47. child.SetActive(true);
  48. }
  49. // Add SteamVR Actions Listener
  50. SteamVR_Actions.default_Menu.AddOnStateDownListener(MenuActionHandler,SteamVR_Input_Sources.Any);
  51. SteamVR_Actions.default_ModeMenu.AddOnStateDownListener(ModeMenuHandler,SteamVR_Input_Sources.Any);
  52. }
  53. // Start is called before the first frame update
  54. void Start()
  55. {
  56. }
  57. // Update is called once per frame
  58. void Update()
  59. {
  60. if(interactionManagement.Robot_Locked){
  61. signal = new Vector2(0,0);
  62. lock_robot_Toggle.isOn = true;
  63. navMeshAgent.ResetPath();
  64. }
  65. else{
  66. UpdateSignal();
  67. lock_robot_Toggle.isOn = false;
  68. }
  69. }
  70. public void UpdateSignal()
  71. {
  72. if(!userControl){
  73. signal = Vector2.zero;
  74. }
  75. // Auto driving
  76. if(follow_me_Toggle.isOn){
  77. Vector3 targetPosition = player.transform.position;
  78. targetPosition.y = robot.transform.position.y;
  79. navMeshAgent.SetDestination(targetPosition);
  80. navMeshAgent.speed = originSpeed * velocity_Slider.value;
  81. }
  82. else{
  83. // stop auto driving
  84. navMeshAgent.ResetPath();
  85. }
  86. }
  87. // Handle Button Select Effect: Left or Right
  88. // angele = -1/1
  89. public void setAngleStart(float angle){
  90. if(interactionManagement.Robot_Locked){
  91. interactionManagement.SetPlayerText("Please unlock the robot");
  92. }
  93. else{
  94. userControl = true;
  95. this.signal = new Vector2(angle,0);
  96. }
  97. }
  98. // Handle Button Select Effect: Forward or Backward
  99. // velocity = -1/1
  100. public void setVelocityStart(float velocity){
  101. if(interactionManagement.Robot_Locked){
  102. interactionManagement.SetPlayerText("Please unlock the robot");
  103. }
  104. else{
  105. userControl = true;
  106. this.signal = new Vector2(0, velocity * velocity_Slider.value);
  107. }
  108. }
  109. public void setControlEnde(){
  110. this.signal = new Vector2(0,0);
  111. userControl = false;
  112. }
  113. public void lockRobot(){
  114. interactionManagement.LockRobot();
  115. }
  116. private void MenuActionHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  117. {
  118. interactionManagement.SetMenu();
  119. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  120. }
  121. private void ModeMenuHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  122. {
  123. bool active = false;
  124. foreach(GameObject child in ModeMenu){
  125. active = !child.activeSelf;
  126. child.SetActive(!child.activeSelf);
  127. }
  128. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  129. }
  130. private void OnDisable() {
  131. foreach(GameObject child in ModeMenu){
  132. if(child != null)
  133. child.SetActive(false);
  134. }
  135. navMeshAgent.speed = originSpeed;
  136. // Remove SteamVR Actions Listener
  137. SteamVR_Actions.default_Menu.RemoveOnStateDownListener(MenuActionHandler,SteamVR_Input_Sources.Any);
  138. SteamVR_Actions.default_ModeMenu.RemoveOnStateDownListener(ModeMenuHandler,SteamVR_Input_Sources.Any);
  139. }
  140. }