- Home /
How do I determine the rotation of the XR unity controller if it is held upright or held facing down
I am trying to determine if my xr controller is held upright or held facing downwards
I have tried "vrhand.transform.rotation.z" which gives me a false value and also "vrhand.transform.eulerAngles.z" which constantly gives me a zero value.
How do I get if the controller is held upright or held facing downwards?
Answer by jackmw94 · Nov 10, 2020 at 04:17 PM
A good way to do this is to find the angle between the direction in which your controller is pointing and the direction in which you're testing against:
Vector3 transformForward = vrhand.transform.forward;
Vector3 up = Vector3.up;
float degreesFromUp = Vector3.Angle(transformForward, up);
if ( degreesFromUp < 10f )
{
// is pointing up
}
The 10f is an arbitrary value, a smaller value will mean you have to be closer to pointing directly up to pass the check whereas a greater value will result in a more relaxed check.
Answer by rh_galaxy · Nov 10, 2020 at 05:00 PM
In addition to what jackmw94 said, if you are using the new XR plugin system you can retrieve the direction like this.
UnityEngine.XR.InputDevice handR = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
handR.TryGetFeatureValue(UnityEngine.XR.CommonUsages.deviceRotation, out Quaternion rotR);
direction = rotR * Vector3.forward;