Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
0
Question by Hyperactive · Jul 24, 2017 at 01:17 PM · rotationlerpeuleranglesmathfrotatearound

Rotation Direction

Hi All,

I've been looking around for a while and can't find any definitive answer for how to rotate in a certain direction. So I've done a bit of testing and exploring different ways to make this work.

In this example, the currentRotation is 0, so the rotation goes clockwise to -90. Which is great.

 public Vector3 currentRotation;
 
    while (true)
         {
             currentRotation.y = Mathf.Lerp(currentRotation.y, -90, Time.deltaTime);
             transform.eulerAngles = currentRotation;
             yield return null;
         }

Though when I do this afterwards, once the currentRotation is now -90 and I want to go back to 0 it takes the long way round, so goes all the way down to -359 then onto 0.

public Vector3 currentRotation;

    while (true)
         {
             currentRotation.y = Mathf.Lerp(currentRotation.y, 0, Time.deltaTime);
             transform.eulerAngles = currentRotation;
             yield return null;
         }

Now my solution is to try and use rotateAround with a Vector3 direction such as:

     transform.RotateAround(transform.position, Vector3.down,  Time.deltaTime);    

This allows me to choose the direction. The issue here now is I can't get the rotation to stop, considering I want to go to -90 how do I make this stop? As doing something like this doesn't work, as it's a negative value:

 while (transform.localEulerAngles.y > -90)
             {
             transform.RotateAround(transform.position, Vector3.down,  Time.deltaTime);           
             yield return null;
         }      

I hope this all makes sense and someone can possibly help with a solution somewhere! Going to try different rotation methods to see if the direction works differently. Some people say Lerp takes the quickest route always, but this can't be true otherwise my rotation wouldn't take the long way round to go from -90 to 0.

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
1
Best Answer

Answer by MattG54321 · Jul 25, 2017 at 01:17 PM

I've deleted my earlier answer because it was incorrect. I've had success with Quaternion.Lerp in the past, but I admit I'm no expert in quaternions. Try Quaternion.RotateTowards.

 public float turningRate = 30f; //Max turn rate in degrees per second
 
 private Quaternion targetRotation = Quaternion.Euler(transform.eulerAngles.x, -90f, transform.eulerAngles.z);
 
     while (true)
         {
             transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, turningRate * Time.deltaTime);
         }

Thanks to DMG's answer on an earlier post. He goes more in depth on how , and more importantly why, you should use quaternions instead of vector3s when dealing with rotation.

I've tried to fit the code to your specific use case, but it may be wiser to just use DMG's code here:

 // Maximum turn rate in degrees per second.
 public float turningRate = 30f; 
 // Rotation we should blend towards.
 private Quaternion _targetRotation = Quaternion.identity;
 // Call this when you want to turn the object smoothly.
 public void SetBlendedEulerAngles(Vector3 angles)
 {
   _targetRotation = Quaternion.Euler(angles);
 }
 
 private void Update()
 {
    // Turn towards our target rotation.
    transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, turningRate * Time.deltaTime);
 }
Comment
Add comment · Show 1 · 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 Hyperactive · Jul 25, 2017 at 03:08 PM 0
Share

Nice one! Your solution definitely worked. So RotateTowards then takes the quickest route? Interesting.

avatar image
0

Answer by BillyBobBeavis · Jun 24, 2021 at 05:08 PM

The following worked for my purposes. All the code does is tell me if my gameObject is rotating clockwise or counterclockwise if Vector3.up is pointing toward the camera.

         angle = Vector3.SignedAngle(transform.forward, preVector, Vector3.up);
         preVector = transform.forward;
 
         if (angle * -1 > 0)
             rotationDirection = 1;
         if (angle * -1 < 0)
             rotationDirection = -1;
        
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

92 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

Related Questions

keep player centred in grid based movement 0 Answers

Keep "ball" character's face rotating to be up? 0 Answers

Quaternion Orientation changing axis problem. 0 Answers

Make GameObject rotate around another around random axis but with fixed distance 0 Answers

Questions about Rotations in unity 1 Answer


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