0
\$\begingroup\$

Hey I would like an object to look at another object only at one axis, when I rotate the white cube all around the gray cube (gray has look at script), the gray cube flips as soon as the target passes the middle and is on the right side. How can I fix this, so that rotation is normal.

Below is when the cube (target, white).

enter image description here

Below is what happens when the cube (target) passes on the other side, as you can see by the texture the gray cube flips horizontally.

enter image description here

Below is how it should be (texture shows difference)

enter image description here

Code:

    Quaternion lookAngle = Quaternion.LookRotation(targetPos, Vector3.up);

Thanks in advance!

\$\endgroup\$
  • \$\begingroup\$ This sounds very similar to the problem solved in "Lookrotation of a gameObject in just one axis" - would the solution there be useful to you? \$\endgroup\$ – DMGregory Apr 1 '17 at 13:04
  • \$\begingroup\$ I'm not sure if it would solve the problem, but I found the solution, I'll post in abit \$\endgroup\$ – Vadim Tatarnikov Apr 1 '17 at 13:09
1
\$\begingroup\$

Solved it, the working code is below:

Vector3 dif = targetPos - transform.position;
    dif.z = 0;

    Quaternion lookAngle = Quaternion.LookRotation(dif, transform.up);
\$\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.