0
\$\begingroup\$

Sooo this question is a problem , a evolution by another solution of this problem here:

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

Basically:

We have a "Eye" (HTC Vive). We have a "Parent" which is the Parent of "Eye".

We can not change the position or rotation of the Eye in code. We can only change the position and rotation of the Parent.

1.We want to modify the Parent Position so that Eye to goes to a certain "Target 1" position.

2.We want to modify the Parent Y-Rotation so that the Eye looks at a certain "Target 2" position.

3.While doing this we dont want to modify the Parent X or Z , because this would lead to a Rotated game experience.

The Parent Position changes so the Eye gets to "Target 1" position. The Parent Position changes so Y- Rotation should look at Target 2 position The Parent X/Z Rotation should not modify the Eye X/Z

\$\endgroup\$
  • \$\begingroup\$ I started a bounty \$\endgroup\$ – OC_RaizW Mar 9 '18 at 12:04
3
+100
\$\begingroup\$
// Construct a scale vector to flatten our directions into the horizontal plane,
// so we only consider the yaw component of the rotation.
var horizontal = new Vector3(1, 0, 1);

// Find which way the eye is pointing in the parent's horizontal plane.
var eyeHeadingHorizontal = Vector3.Scale(eye.localRotation * Vector3.forward, horizontal);
// Find which way we want it to look in the world's horizontal plane.
var lookHeadingHorizontal = Vector3.Scale(target2 - target 1, horizontal);

// Compute a rotation that takes the eye's heading to the desired heading.
parent.rotation = Quaternion.FromToRotation(
             eyeHeadingHorizontal,
             lookHeadingHorizontal);

// Compute the eye's worldspace offset from its parent in this orientation.
Vector3 eyeOffset = parent.TransformVector(eye.localPosition);

// Move the parent so that its position plus this offset puts the eye exactly on-target.
parent.position = target1 - eyeOffset;
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.