0
\$\begingroup\$

Im trying to rotate an object 90 degrees to the left but instead of rotating straight left it rotates 270 degrees to the right.

Here's the part of the code which controls the rotation:

rotationDestination = new Vector3(60, 270, 0);
if (Vector3.Distance(transform.eulerAngles, rotationDestination) > 0.2f) {
  transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, 
                                       rotationDestination, 
                                       rotationSpeed * Time.deltaTime);
}

If I try to make the Y axis -90 a.k.a (60, -90, 0) then it rotates constantly to the left.

How do I make it rotate straight to the left (90 degrees) ?

Thank you.

\$\endgroup\$
2
\$\begingroup\$

You can use Quaternions instead of Euler Angles to solve this.

transform.rotation = Quaternion.Lerp(transform.rotation, 
    Quaternion.Euler(rotationDestination), Time.deltaTime * rotationSpeed);

If you want to test distance still, you can use...

if(Quaternion.Angle(transform.rotation, Quaternion.Euler(rotationDestination)) > 0.2f)

I'd suggest you make 'rotationDestination' into a quaternion too. Just so that you're not converting back and forth between quaternions, and euler angles as much.

| 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.