TestRobot.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. private void FixedUpdate()
  47. {
  48. move();
  49. }
  50. // move the robot in unity
  51. private void move(){
  52. Vector3 linearVelocity = Vector2.zero;
  53. Vector3 angularVelocity = Vector2.zero;
  54. RoboterControl robotControl =this.GetComponent<RoboterControl>();
  55. if(robotControl != null && robotControl.enabled){
  56. Vector2 controlSignal = this.GetComponent<RoboterControl>().getControlSignal();
  57. linearVelocity = new Vector3(0,0,controlSignal.y * linearSpeed);
  58. angularVelocity = new Vector3(0,controlSignal.x * angularSpeed,0);
  59. }
  60. else{
  61. Vector2 controlSignal = GameObject.Find("Input").GetComponent<VRInput>().getSignal();
  62. linearVelocity = new Vector3(0,0,controlSignal.y * linearSpeed);
  63. angularVelocity = new Vector3(0,controlSignal.x * angularSpeed,0);
  64. }
  65. this.transform.Translate(linearVelocity);
  66. this.transform.Rotate(angularVelocity);
  67. }
  68. private void OnTriggerEnter(Collider other) {
  69. if(other.gameObject.tag == "Building"){
  70. triggerCount++;
  71. CollisionAlarm.Play();
  72. vibration.Execute(0f, 0.1f, 50f, 0.5f, SteamVR_Input_Sources.Any);
  73. }
  74. }
  75. }