using UnityEngine; using Valve.VR; using TMPro; public class HeadsetGyroscope : MonoBehaviour { private TextMeshProUGUI gyroText; private uint headsetIndex = OpenVR.k_unTrackedDeviceIndexInvalid; void Start() { gyroText = GetComponent(); if (gyroText == null) { Debug.LogError("TextMeshProUGUI component not found on this GameObject"); } // Initialize OpenVR if not already initialized if (OpenVR.System == null) { OpenVRUtil.System.InitOpenVR(); } // Find the headset device index (should be 0 for the HMD) headsetIndex = OpenVR.k_unTrackedDeviceIndex_Hmd; } void Update() { if (OpenVR.System == null || gyroText == null) return; // Make sure the headset is connected if (!OpenVR.System.IsTrackedDeviceConnected(headsetIndex)) { gyroText.text = "Headset not connected"; return; } // Get the tracking state of the headset TrackedDevicePose_t pose = new TrackedDevicePose_t(); TrackedDevicePose_t[] poses = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount]; OpenVR.System.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseStanding, 0, poses); pose = poses[headsetIndex]; if (!pose.bDeviceIsConnected) { gyroText.text = "Tracking not valid"; return; } // Extract angular velocity (gyroscope data) from the pose Vector3 angularVelocity = new Vector3( pose.vAngularVelocity.v0, pose.vAngularVelocity.v1, pose.vAngularVelocity.v2 ); // Format and display the gyroscope data gyroText.text = string.Format("Gyro:\nX: {0:F2}\nY: {1:F2}\nZ: {2:F2}", angularVelocity.x, angularVelocity.y, angularVelocity.z ); } }