- Home /
Apply game object rotation using relative VR controller rotation?
I have a game object which I want to be able to rotate using my VR controller. I can kind've achieve this by doing the following...
bool triggerClicked = false;
Transform rightHand = Player.instance.rightHand.transform;
void Update () {
if (triggerClicked) {
transform.eulerAngles = rightHand.eulerAngles;
}
{
...but the problem is the object I want to rotate initially snaps to my controller's current rotation when I press the trigger. I need it to move relative to the object's initial rotation instead. What is the correct way to do this?
Answer by AzzyDude24601 · Jan 18, 2021 at 05:32 PM
Solved it. You need to capture some initial quaternions when the trigger is first clicked and then calculate the relative differences and apply them. This prevents the initial snapping to rotation issue.
private bool triggerClicked = false;
private Transform rightHand = Player.instance.rightHand.transform;
private Quaternion initialObjectRotation;
private Quaternion initialControllerRotation;
private bool set = false;
void Update () {
if (triggerClicked) {
if(set == false)
{
initialObjectRotation= transform.rotation;
initialControllerRotation = rightHand .rotation;
set = true;
}
Quaternion controllerAngularDifference = initialControllerRotation * Quaternion.Inverse(rightHand.rotation);
transform.rotation = controllerAngularDifference * initialObjectRotation;
}
else
{
set = false;
}
{
Thanks, I can confirm this worked. Can be simplified a bit, instead of two initialRotation variables, their combined state can be saved in one Quaternion relativeRotation like this:
relativeRotation = Quaternion.Inverse(rightHand.rotation) * transform.rotation;
And the resulting object rotation set like:
transform.rotation = rightHand.rotation * relativeRotation;
Needed like 1000 attempts to figure it out until I ended up with the same solution like yours, then it finally snapped together.
Answer by xogur6889 · Jan 18, 2021 at 04:57 PM
Vector3 firstEuler;
void Start() { firstEuler = transform.eulerAngles; }
update() { ... { transform.eulerAngles = firstEuler + rightHand.eulerAngles; } }
This is still causing an initial "snap" of the rotatable object? As in it jumps to a new initial rotation and then works as intended.