Laser.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Laser.cs
  2. * author: Yannic Seidler
  3. */
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using Valve.VR;
  9. using RosSharp.RosBridgeClient;
  10. // Class that lets the player select a robot for simulation with a laser.
  11. // Attached to GameObject Laser pointer (Scene: SelectRobot).
  12. public class Laser : MonoBehaviour
  13. {
  14. public SteamVR_ActionSet SelectRobot;
  15. public SteamVR_Action_Boolean trigger;
  16. public Transform rightHand;
  17. public float laserLength;
  18. LineRenderer line;
  19. GameObject currentRobotSelected;
  20. private GameObject[] robots;
  21. private RosConnector rosbridge;
  22. // Start is called before the first frame update.
  23. // RosBridge is used to decide whether Ros is connected to Unity. Only than the simulation scene can be started.
  24. void Start()
  25. {
  26. rosbridge = GameObject.Find("RosBridge").GetComponent<RosConnector>();
  27. line = GetComponent<LineRenderer>();
  28. line.startWidth = 0.005f;
  29. line.endWidth = 0.001f;
  30. line.positionCount = 2;
  31. robots = GameObject.FindGameObjectsWithTag("robot");
  32. trigger.onStateDown += Trigger_onStateDown;
  33. SelectRobot.Activate();
  34. }
  35. // Selects a robot and loads the simulation scene with the robot.
  36. private void Trigger_onStateDown(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  37. {
  38. if (currentRobotSelected != null && rosbridge.connected)
  39. {
  40. GameObject.Find("SceneManagement").GetComponent<SceneManagement>().loadSceneAsync
  41. (currentRobotSelected.GetComponent<RobotInformation>().robotType);
  42. Destroy(this);
  43. }
  44. }
  45. //Specified robot gets outlined. Outliner for the other robots in list gets deactivated.
  46. // If no robot is specified the outliner for all robots gets deactivated.
  47. private void OutlineRobot(robotType robotToOutline, GameObject[] robots)
  48. {
  49. foreach (GameObject robot in robots)
  50. {
  51. if (robotToOutline != robotType.noRobot)
  52. {
  53. if (robot.GetComponent<RobotInformation>().robotType == robotToOutline)
  54. {
  55. robot.GetComponent<Outline>().enabled = true;
  56. continue;
  57. }
  58. }
  59. robot.GetComponent<Outline>().enabled = false;
  60. }
  61. }
  62. // Update is called once per frame.
  63. // Laser logic: renders laser in scene.
  64. // If the laser hits the robot the robot gets outlined. If you pull the trigger the robot gets selected.
  65. void Update()
  66. {
  67. transform.position = rightHand.position;
  68. transform.rotation = rightHand.rotation;
  69. Vector3 point1 = rightHand.position;
  70. Vector3 point2;
  71. Ray ray = new Ray(point1, rightHand.TransformDirection(Vector3.forward));
  72. RaycastHit hit;
  73. bool hitted = Physics.Raycast(ray, out hit, laserLength);
  74. if(hitted)
  75. {
  76. if (hit.transform.root.tag == "robot")
  77. {
  78. switch (hit.transform.root.gameObject.GetComponent<RobotInformation>().robotType)
  79. {
  80. case robotType.drz_telemax:
  81. OutlineRobot(robotType.drz_telemax, robots);
  82. break;
  83. case robotType.asterix_ugv:
  84. OutlineRobot(robotType.asterix_ugv, robots);
  85. break;
  86. }
  87. currentRobotSelected = hit.transform.root.gameObject;
  88. }
  89. else
  90. {
  91. OutlineRobot(robotType.noRobot, robots);
  92. currentRobotSelected = null;
  93. }
  94. point2 = hit.point;
  95. }
  96. else
  97. {
  98. point2 = point1 + rightHand.TransformDirection(Vector3.forward * laserLength);
  99. OutlineRobot(robotType.noRobot, robots);
  100. currentRobotSelected = null;
  101. }
  102. Vector3 [] points = new Vector3 [2] {point1 , point2};
  103. line.SetPositions(points);
  104. }
  105. // Removes the selection event.
  106. private void OnDestroy()
  107. {
  108. trigger.onStateDown -= Trigger_onStateDown;
  109. SelectRobot.Deactivate();
  110. }
  111. }