• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by RodDQ · Aug 16, 2017 at 07:25 PM · rotationquaternionsrotatetowards

Quaternion.RotateTowards turning the wrong way

I'm writing a script to make an object follow a sequence of points. It turns towards the next point and then moves toward it until it gets there. Then it repeats for the next point.

Sometimes, however, it turns the wrong way, Quaternion.RotateTowards rotates more than 180 degrees to turn towards the new target. Here's the core of the code. Points is a list of Vector3.

void Update() { // See if we are pointing toward the target point. Vector3 targetPoint = Points[_currentPoint]; Vector3 targetDirection = targetPoint - transform.position; Quaternion targetRotation = Quaternion.LookRotation(targetDirection);

 float angle = Quaternion.Angle(transform.rotation, targetRotation);

 if (Mathf.Abs(angle) > 0.01f)
 {
     // Rotate toward the target.
     transform.rotation = Quaternion.RotateTowards(
         transform.rotation, targetRotation,
         rotationSpeed * Time.deltaTime);
 }
 else if (targetDirection.magnitude > 0.01f)
 {
     // Move toward the target.
     transform.position = Vector3.MoveTowards(
         transform.position, targetPoint, moveSpeed * Time.deltaTime);
 }
 else
 {
     // We are at the target. Go to the next one.
     _currentPoint = (_currentPoint + 1) % Points.Count;
 }

}

Any thoughts on why Quaternion.RotateTowards would sometimes not use the shortest rotation?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AfroDieter · Feb 19, 2018 at 02:38 PM

I am trying to figure the solution to this one out myself right now. I think I know however why it is sometimes rotation the wrong way: If you want to rotate to Vector3.zero (looking to the right side from above) and you looked to Vector3(0,90,0) before it is all fine. However if you looked to Vector3(0,-90,0) before it will not have it stored that way many times. instead it will say you were looking to Vector3(0,270,0) hence rotating you 270 degrees in the wrong direction instead of 90 in the correct direction.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by U92 · Aug 20, 2020 at 09:26 AM

2020 still a problem any one find a solution

 Vector3[] blockRotation = new[] {
     new Vector3(90, 0,  0),
     new Vector3(90,  30, 0),
     new Vector3(90,  60, 0),
     new Vector3(90,  90, 0),
     new Vector3(90,  120, 0),
     new Vector3(90,  150, 0),
     new Vector3(90,  180, 0),
     new Vector3(90,  210, 0),
     new Vector3(90,  240, 0),
     new Vector3(90,  270, 0),
     new Vector3(90,  300, 0),
     new Vector3(90,  330, 0)


 };


90 and 270 break, been stuck on this for 6+ hours ZERO SOLUTIONS

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object rotate and slowly stop to toward 1 Answer

Rotate y-axis of child object following a rigidbody sphere parent 0 Answers

Why does RotateTowards rotate in an "orbit" rather than on the spot? Help me understand! 0 Answers

Get object's rotation around axis defined by vector 2 Answers

Why is this multiplication of two quaternion rotations not working correctly 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges