TestRobot.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.AI;
  6. using UnityEngine.SceneManagement;
  7. using Valve.VR;
  8. public class TestRobot : MonoBehaviour
  9. {
  10. [Tooltip("The speed of the robot")]
  11. public float linearSpeed = 0.1f;
  12. [Tooltip("The speed of the robot's angle change")]
  13. public float angularSpeed = 1f;
  14. // ------- Test Information-------------
  15. [Tooltip("Number of collisions detected")]
  16. public int triggerCount = 0;
  17. [Tooltip("Total driving distance. (meter)")]
  18. public float totalDriveDistance = 0;
  19. [Tooltip("Total driving time. (second)")]
  20. public float totalDriveTime = 0;
  21. [Tooltip("Adverage speed. (m/s)")]
  22. public float averageSpeed = 0;
  23. // --------------
  24. private Vector3 PrePosition;
  25. public AudioSource CollisionAlarm;
  26. SteamVR_Action_Vibration vibration = SteamVR_Input.GetVibrationAction("Haptic");
  27. // Start is called before the first frame update
  28. void Start()
  29. {
  30. PrePosition = transform.position;
  31. }
  32. // Update is called once per frame
  33. void Update()
  34. {
  35. move();
  36. // caculate the distance
  37. totalDriveDistance += Vector3.Distance(PrePosition,transform.position);
  38. PrePosition = transform.position;
  39. // caculate the time
  40. Vector2 controlSignal = GameObject.Find("Input").GetComponent<VRInput>().getSignal();
  41. if(controlSignal!=Vector2.zero || this.GetComponent<NavMeshAgent>().velocity!=Vector3.zero){
  42. totalDriveTime += Time.deltaTime;
  43. averageSpeed = totalDriveDistance/totalDriveTime;
  44. }
  45. }
  46. // move the robot in unity
  47. private void move(){
  48. Vector3 linearVelocity = Vector2.zero;
  49. Vector3 angularVelocity = Vector2.zero;
  50. RoboterControl robotControl =this.GetComponent<RoboterControl>();
  51. if(robotControl != null && robotControl.enabled){
  52. Vector2 controlSignal = this.GetComponent<RoboterControl>().getControlSignal();
  53. linearVelocity = new Vector3(0,0,controlSignal.y * linearSpeed);
  54. angularVelocity = new Vector3(0,controlSignal.x * angularSpeed,0);
  55. }
  56. else{
  57. Vector2 controlSignal = GameObject.Find("Input").GetComponent<VRInput>().getSignal();
  58. linearVelocity = new Vector3(0,0,controlSignal.y * linearSpeed);
  59. angularVelocity = new Vector3(0,controlSignal.x * angularSpeed,0);
  60. }
  61. this.transform.Translate(linearVelocity);
  62. this.transform.Rotate(angularVelocity);
  63. }
  64. private void OnTriggerEnter(Collider other) {
  65. if(other.gameObject.tag == "Building"){
  66. triggerCount++;
  67. CollisionAlarm.Play();
  68. vibration.Execute(0f, 0.1f, 50f, 0.5f, SteamVR_Input_Sources.Any);
  69. }
  70. }
  71. }