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\$
2
  • \$\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
    Commented Apr 1, 2017 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\$
    – John Smith
    Commented Apr 1, 2017 at 13:09

1 Answer 1

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\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .