- Home /
Change rotation of an object based on an HTC Vive Controller
I'm having difficulty trying to alter the position of an object which I'm moving based on an HTC Vive controller. I want the object to follow the controller, which I've achieved by making the object's position the same as the controller:
gameObject.transform.localPosition = new Vector3(viveController.transform.localPosition.x, viveController.transform.localPosition.y, viveController.transform.localPosition.z);
For the rotation I don't want to completely copy the controller. I want the object (imagine a long rectangle) to be pointing upwards at 45 degrees at all time, which I believe is the X axis, while mirroring the Y and Z axis. I'm doing this by:
gameObject.transform.eulerAngles = new Vector3(45.0f, viveController.transform.localRotation.eulerAngles.y, viveController.transform.localRotation.eulerAngles.z);
Which works as I expect if I'm holding the controller flat or pointing slightly upwards to the ceiling, but as soon as I point the controller down towards the floor the object's 45 degree angle pointing upwards flips (mirrors) to 45 degrees pointing down.
I believe this is because the three axes don't appear to be individually changed, i.e. a change to the X axis in the 3D space will also change the Y and Z axes as well. What is happening and how can I achieve what I want? The object should always be pointing upwards regardless of the controllers orientation. Any advice on how to change a single axis without changing the other two would be appreciated!
I've tried a few things including trying localEulerAngles and some Quaternion stuff but I don't really know what to search online to solve my issue. I'm trying to get the object to point at a global 45 degrees, and not relative to the controller. Thanks!
Answer by XpctD · Mar 31, 2021 at 05:31 PM
Hi there @w88dy . Is it possible to add a rotation constraint component? Here is an example I used:
I'm glad I'm not the only one that responds to 4 year old threads =)
Ahahaha oops, I just saw it in related questions and didn't bother to check.
I believe that would work though, haven't played with Vive in Unity in awhile though.
Your answer
Follow this Question
Related Questions
Why is this rotation not performed as expected? 1 Answer
When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers
Camera viewport transformation from one world to the rotated world. 1 Answer
My Obstacle Rotations Are Not Correct 0 Answers
Code to rotate around a local axis until local Y is 0? 0 Answers