Hello everyone.
I would like a UI element to open when I rotate the Vive controller 90 degrees. I have seen a lot of games do this already, but I have no idea on how to track the rotation of the controller.
The UI would be attached to the controller, but not visible until you flip the controller.
How can this be done?
Any help would be appreciated ![]()
If you mean a “wristwatch” style UI, it may be helpful to think of it more as when the wrist is pointing towards the headset, rather than which way is facing up. Either way, though, the logic is basically the same - in my sample code below, just use Vector3.up instead of headVector, and point the transform a different direction.
Create an object which will be the root of your UI, as a child of the controller object, and place it at the wrist, with its Z axis facing “out” (like a stick poking out from a wristwatch) - in my experience, you want this slightly behind the plug end of the controller, sticking out from its left side. Attach a script to this object. From there, you can do something like:
public Transform head; //the headset object
public float appearAngle = 10f;
public float disappearAngle = 20f; //these are different numbers to avoid "flickering" at the edge of the range
public GameObject ui; //a child of this object, containing the UI objects
void LateUpdate() {
Vector3 wristVector = transform.forward;
Vector3 headVector = head.position - transform.position;
float thisAngle = Vector3.Angle(wristVector, headVector);
if (!ui.activeInHierarchy && thisAngle < appearAngle) ui.SetActive(true);
if (ui.activeInHierarchy && thisAngle > disappearAngle) ui.SetActive(false);
}