Hey everyone,
As the title says, I wanted to ask if anyone knew how to detect how much my oculus quest 2 controller is tilted in unity.
I want to be able to control ama airplane in my game and therefore fly it kind of like with a joystick (this being the controller starting from a vertical position).
So yeah, in unity (newest version now so 2021.Something) is there a way to get the “rotation” Of controllers?
Thank you so much,
Matteo
This code is using the new input system on the Quest 2 device. It should also work after a recentering of the HMD.
using UnityEngine.XR;
using UnityEngine.InputSystem;
public class HandControllers : MonoBehaviour
{
internal Vector3 vPointPosition;
internal Vector3 vPointDirection;
//internal Quaternion qRotation;
private void Update()
{
UnityEngine.XR.InputDevice handRDevice = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
UnityEngine.XR.InputDevice handLDevice = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
//the right controller, the left is the same but using handLDevice
bool posRSupported = handRDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.devicePosition, out Vector3 posR);
vPointPosition = transform.TransformPoint(posR); //to world coords
bool rotRSupported = handRDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.deviceRotation, out Quaternion rotR);
vPointDirection = rotR * Vector3.forward;
vPointDirection = transform.TransformDirection(vPointDirection);
//qRotation = Quaternion.LookRotation(vPointDirection);
//...
}
}
@rh_galaxy Thanks so much for having took the time to answer! I will try this tomorrow and tell you if it works. Thanks again!