# How can I change the parents rotation so the childs rotation is looking at a specific direction

I have a child object which is always having different positions and rotations (thats my VR Headsets Eye-Gameobject) I cannot modify the position and rotation of that.

I want to set my view to a certain "startposition" and "startrotation" so the player always has the same view at the start of the game.

The childs local position is always set to some values. So I cannot unparent it. If I unparent it, I change his position.

now..

It works with the position.

Parent.transform.position =  2 * ParentStartPostion - Eye.localPosition;


^ This line of code here sets my parent object so that my Gameobject: "eye-camera" is where i want it to be.

For the rotation part, i would do following:

Eye.parent = null; //unparent the eye to the world
Parent.transform.position = Eye.position; //set my Parent to the same position as Eye
Eye.parent = transform; //parent it back. Local Eye should be 0.0.0 now.
Parent.transform.rotation = Quaternion.LookRotation(StartForward, StartUp); // i can rotate the at the direction I want it.


But after I unparent my Eye the position of the local(which is also global) is set to a different value. And after I parent it back, its set to the old value again.

Eye.parent = null; //unparent my eye to the world - Eye moves anyway because its movend by SteamVR
Parent.transform.position = Eye.position; //set my Parent to the same position as Eye (works)
Eye.parent = transform; //parent it back. Local Eye moves back where it was and there is a distance between parent and eye again.
Parent.transform.rotation = Quaternion.LookRotation(StartForward, StartUp); // the rotation changing my eyes position because its parented. Also the rotation is wrong.


And now i dont know anymore how to solve this problem.

so... i painted my problem in paint for better understanding:

// Populate the net rotation that you want the child to have.
Quaternion desiredRotation = Quaternion.LookRotation(StartForward, StartUp);

// Create a rotation that undoes the child's rotation, then applies the desired rotation.
Quaternion rotationCorrection = desiredRotation * Quaternion.Inverse(Eye.localRotation);

// The parent will apply this correction to its child transforms.
Parent.transform.rotation = rotationCorrection;

// After this rotation has been applied,
// find offset from the child to the position we want it to start, in worldspace.
Vector3 positionCorrection = desiredPosition - Eye.position;

// Apply this offset to the parent's worldspace position to shift it into place.
Parent.transform.position = Parent.transform.position + positionCorrection;


Just be careful when creating mismatches between the true position/movement of the player's head and the perspective in-game. Be up-front with the player about what pose they should have when starting the game.

You may want to consider stripping out some of the components you calibrate for at startup:

• Ignore the pitch and roll of the head (so that if I start the game with my head slightly tilted, this doesn't tilt the whole world sideways for the rest of the game)

• Ignore the vertical offset in the position correction (so that if I start the game sitting, then stand up, I'm not levitating for the rest of the game)

I can show you how to apply these changes if they're useful for your case.

• works like a charm! Thx buddy – OC_RaizW Jun 19 '17 at 8:23
• How to achieve this? @DMGregory Ignore the pitch and roll of the head (so that if I start the game with my head slightly tilted, this doesn't tilt the whole world sideways for the rest of the game) It doesnt work with my solution: Vector3 eyeWithoutYawAndRollEuler = new Vector3(0.0f, Eye.eulerAngles.y, 0.0f); Quaternion eyeWithoutYawAndRollQuaternion = Quaternion.Euler(eyeWithoutYawAndRollEuler); – OC_RaizW Jun 20 '17 at 6:09