Advertisement
Guest User

Untitled

a guest
Jun 28th, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.XR;
  6.  
  7. public class DroneMovement : MonoBehaviour
  8. {
  9.  
  10. private InputDevice headsetDevice; // Variable to store the VR headset device
  11. //Vector3 headsetPosition;
  12. Vector3 previousPosition;
  13. Vector3 initialposition;
  14. public TMP_Text speedText;
  15.  
  16.  
  17. public float maxSpeedKPH = 15f;
  18. public int speed = 8;
  19.  
  20. private float speedKPH;
  21.  
  22. // Start is called before the first frame update
  23. void Start()
  24. {
  25. speedKPH = 0f;
  26. assignHeadsetDevice();
  27. }
  28.  
  29.  
  30. // Update is called once per frame
  31. void Update()
  32. {
  33. if (!headsetDevice.isValid)
  34. {
  35. assignHeadsetDevice();
  36. }
  37.  
  38. if (headsetDevice.isValid)
  39. {
  40.  
  41. if (headsetDevice.TryGetFeatureValue(CommonUsages.devicePosition, out Vector3 headsetPosition))
  42. {
  43. if (initialposition == Vector3.zero)
  44. initialposition = headsetPosition;
  45.  
  46. //how much distance was done
  47. Vector3 positionOffset = headsetPosition - initialposition;
  48.  
  49.  
  50. // Calculate the speed using the CalculateSpeed function
  51. speedKPH = CalculateSpeed(positionOffset, previousPosition, Time.deltaTime);
  52.  
  53.  
  54. // Limit the speed to a maximum of 15 km/h
  55. if (speedKPH > maxSpeedKPH)
  56. {
  57. Debug.Log("MAX SPEED REACHED: " + speedKPH);
  58. //positionOffset = positionOffset.normalized * (maxSpeedKPH * 2 * 1000f / 3600f) * Time.deltaTime;
  59. }
  60.  
  61. positionOffset.y = 0f;
  62.  
  63. transform.Translate(positionOffset * Time.deltaTime * speed, Space.World);
  64. // actual movement
  65. //transform.Translate(positionOffset.normalized * speed * Time.deltaTime, Space.World);
  66.  
  67. //new shit from gpt
  68. // transform.Translate(positionOffset * Time.deltaTime * speed, Space.World);
  69.  
  70.  
  71.  
  72. // SPEED TRACKING
  73.  
  74. // Store the current position
  75. Vector3 currentPosition = transform.position;
  76.  
  77. // Calculate the time elapsed since the last frame
  78. float deltaTime = Time.deltaTime;
  79.  
  80. // Calculate the speed using the CalculateSpeed function
  81. speedKPH = CalculateSpeed(currentPosition, previousPosition, deltaTime);
  82.  
  83.  
  84.  
  85. // Store the current position for the next frame
  86. previousPosition = currentPosition;
  87.  
  88. // Output the speed to the console
  89. speedText.SetText("Speed: " + Mathf.Round(speedKPH) + " km/h");
  90.  
  91.  
  92. //speedText.SetText("Speed: " + speedKPH.ToString("F1") + " km/h");
  93.  
  94. if (speedKPH >= 15)
  95. speedText.color = Color.blue;
  96. else
  97. speedText.color = Color.white;
  98.  
  99. }
  100.  
  101.  
  102.  
  103. Quaternion headsetRotation;
  104. if (headsetDevice.TryGetFeatureValue(CommonUsages.deviceRotation, out headsetRotation))
  105. {
  106. // Use headsetRotation for tracking the rotation of the headset
  107. }
  108. }
  109.  
  110.  
  111. }
  112.  
  113.  
  114.  
  115. private float CalculateSpeed(Vector3 currentPosition, Vector3 previousPosition, float deltaTime)
  116. {
  117. // Calculate the distance traveled between the current and previous positions
  118. float distance = Vector3.Distance(currentPosition, previousPosition);
  119.  
  120. // Calculate the speed in km/h
  121. float speedKPH = (distance / 1000) / (deltaTime / 3600);
  122.  
  123. return speedKPH;
  124. }
  125.  
  126.  
  127. void assignHeadsetDevice()
  128. {
  129. var inputDevices = new List<UnityEngine.XR.InputDevice>();
  130. UnityEngine.XR.InputDevices.GetDevices(inputDevices);
  131. foreach (var device in inputDevices)
  132. {
  133. // Debug.Log(string.Format("Device found with name '{0}' and role '{1}'", device.name, device.characteristics.ToString()));
  134. headsetDevice = device;
  135.  
  136.  
  137. }
  138.  
  139. Debug.Log("Characteristics: " + headsetDevice.characteristics);
  140. }
  141.  
  142.  
  143. }
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement