Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.XR;
- public class DroneMovement : MonoBehaviour
- {
- private InputDevice headsetDevice; // Variable to store the VR headset device
- //Vector3 headsetPosition;
- Vector3 previousPosition;
- Vector3 initialposition;
- public TMP_Text speedText;
- public float maxSpeedKPH = 15f;
- public int speed = 8;
- private float speedKPH;
- // Start is called before the first frame update
- void Start()
- {
- speedKPH = 0f;
- assignHeadsetDevice();
- }
- // Update is called once per frame
- void Update()
- {
- if (!headsetDevice.isValid)
- {
- assignHeadsetDevice();
- }
- if (headsetDevice.isValid)
- {
- if (headsetDevice.TryGetFeatureValue(CommonUsages.devicePosition, out Vector3 headsetPosition))
- {
- if (initialposition == Vector3.zero)
- initialposition = headsetPosition;
- //how much distance was done
- Vector3 positionOffset = headsetPosition - initialposition;
- // Calculate the speed using the CalculateSpeed function
- speedKPH = CalculateSpeed(positionOffset, previousPosition, Time.deltaTime);
- // Limit the speed to a maximum of 15 km/h
- if (speedKPH > maxSpeedKPH)
- {
- Debug.Log("MAX SPEED REACHED: " + speedKPH);
- //positionOffset = positionOffset.normalized * (maxSpeedKPH * 2 * 1000f / 3600f) * Time.deltaTime;
- }
- positionOffset.y = 0f;
- transform.Translate(positionOffset * Time.deltaTime * speed, Space.World);
- // actual movement
- //transform.Translate(positionOffset.normalized * speed * Time.deltaTime, Space.World);
- //new shit from gpt
- // transform.Translate(positionOffset * Time.deltaTime * speed, Space.World);
- // SPEED TRACKING
- // Store the current position
- Vector3 currentPosition = transform.position;
- // Calculate the time elapsed since the last frame
- float deltaTime = Time.deltaTime;
- // Calculate the speed using the CalculateSpeed function
- speedKPH = CalculateSpeed(currentPosition, previousPosition, deltaTime);
- // Store the current position for the next frame
- previousPosition = currentPosition;
- // Output the speed to the console
- speedText.SetText("Speed: " + Mathf.Round(speedKPH) + " km/h");
- //speedText.SetText("Speed: " + speedKPH.ToString("F1") + " km/h");
- if (speedKPH >= 15)
- speedText.color = Color.blue;
- else
- speedText.color = Color.white;
- }
- Quaternion headsetRotation;
- if (headsetDevice.TryGetFeatureValue(CommonUsages.deviceRotation, out headsetRotation))
- {
- // Use headsetRotation for tracking the rotation of the headset
- }
- }
- }
- private float CalculateSpeed(Vector3 currentPosition, Vector3 previousPosition, float deltaTime)
- {
- // Calculate the distance traveled between the current and previous positions
- float distance = Vector3.Distance(currentPosition, previousPosition);
- // Calculate the speed in km/h
- float speedKPH = (distance / 1000) / (deltaTime / 3600);
- return speedKPH;
- }
- void assignHeadsetDevice()
- {
- var inputDevices = new List<UnityEngine.XR.InputDevice>();
- UnityEngine.XR.InputDevices.GetDevices(inputDevices);
- foreach (var device in inputDevices)
- {
- // Debug.Log(string.Format("Device found with name '{0}' and role '{1}'", device.name, device.characteristics.ToString()));
- headsetDevice = device;
- }
- Debug.Log("Characteristics: " + headsetDevice.characteristics);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement