- Home /
Apply PitchRotation to object based on the rotation of another object
I have an object A, that can have a starting rotation of anything. I have a second object B that points at A that has a variable pitch up/down relative to A. I would like to apply only the pitch of B pointed at A(I dont want roll or yaw).
A physical example would be a laser pointer pointing at a wall, if you pitch the laser pointer down, the wall has the same pitch applied mirroring the laser pointer. But I dont want the roll or yaw to effect the wall.
The real use case here is that I am building a VR interaction system where the final component to the movement/rotation is the ability to pitch the rotation of a held object based on the players hand(two actually). I have everything working from rotation about the player and rotation relative to both hands, but I just cant get the hand pitch to work which would complete the process.
I would love to just apply the world rotation of the controller straight to the object im grabbing but I cant because if you rotate so that you are facing backwards, the whole thing breaks, I can only use the pitch rotation on the target object. This has been fiendishly difficult, probably because I am not a superstar at quaternions yet. Here is my current block of code that I am playing with, this sort of works except when I face a particular angle, the x axis value just doesnt change even when I drop it into local rotation.
// Getting the rotation based in the change of the controller up vector
Quaternion leftQuat = Quaternion.FromToRotation(oHintLeft, leftTrans.up);
//Quaternion rightQuat = Quaternion.FromToRotation(oHintRight, rightTrans.up);
// Cheat to get the leftQuat rotation into local rotation for the object im
// trying to pitch
Quaternion objRotation = objectToGrab.rotation;
objectToGrab.rotation = leftQuat;
Quaternion localRotation = objectToGrab.localRotation;
objectToGrab.rotation = objRotation;
// older attempt to get the local relative rotation
//leftQuat = Quaternion.Inverse(gameObjectRef.transform.rotation) * leftQuat;
// attempting to remove all the other axis values except for x(which should be pitch in local
leftQuat = Quaternion.Euler(localRotation.eulerAngles.x, 0, 0);
//rightQuat = Quaternion.Euler(rightQuat.eulerAngles.x, 0, 0);
// Finally applying the pitch rotation to the local rotation
objectToGrab.localRotation = originalLocalRotation * leftQuat;
Any insight would save me some hair loss thank you!
Your answer
Follow this Question
Related Questions
Disable Windows Mixed Reality headset rotation/position tracking 1 Answer
How to flip the Steam VR Play Area when loading a new scene? 0 Answers
Android: how do i make the rotation of my player follow the rotation of my phone 1 Answer
Rotating Cube with oculus touch controllers 0 Answers
Setting camera pitch - Never works? 0 Answers