Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
2
Question by Vicas · Feb 19, 2013 at 08:43 AM · rotationquaternionlerp

Controlling the direction of a rotation

I have a dial with a needle, somewhat resembling a gauge of some kind. I need the needle to always turn the same way around, that is clockwise.

When my needle is at min say -90 and I want it to go to max, say +90, the dial goes counter-clockwise since this is shorter.

Given this code, which turns a transform, how would I force the rotation to take the other way around?

     [ContextMenu("Step on it")]
     void Test()
     {
         StartCoroutine(StartAcceleration());
     }
     
     IEnumerator StartAcceleration()
     {
         Quaternion fromRotation = this.transform.localRotation;
         Quaternion toRotation = Quaternion.Euler(0,0,-90);
         
         float seconds = 3.0f;
         float rate = 1.0f / seconds;
         
         float lerpFactor = 0;
         while (lerpFactor < 1.0f)
         {
             this.transform.localRotation = Quaternion.Slerp(fromRotation, toRotation , lerpFactor);
             
             lerpFactor += Time.deltaTime * rate;
             
             yield return null;
         }
     }

I have tried Lerping with angles, but I have trouble figuring out how to do the lerp, and what to define in the while conditions, since my angle wraps half-way through from 0 to 360.

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

Answer by Loius · Feb 19, 2013 at 09:40 AM

You could lerp the angle (single float) instaed of the entire rotation. Lerp from your min angle to max angle and just directly set ,rotation = Quaternion.Euler(0,angle,0).

There shouldn't be any issue with wrapping from 0 to 360; those are the same rotation (and so are 720 and -360, etc).

(you can't force slerp to go a different direction; it's wired to take the shortest route, just like lerp is (though, understandably, it's harder to tell with lerp))

Comment
Add comment · Show 6 · 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 Vicas · Feb 19, 2013 at 01:57 PM 0
Share

Could you try giving me an example in the code? Since I have tried this before, but I am not sure about how it should be done.

What do I use to lerp on as the angle ins$$anonymous$$d of roRotation? Since this is a Quaternion and Quaternion.Slerp needs two Quaternions and not a float angle.

avatar image Loius · Feb 20, 2013 at 01:36 AM 0
Share

Don't slerp at all, just lerp the single axis of rotation.

Since you're only rotating around Y you just need to lerp from -90 to 90 and substitute that for angle.

transform.rotation = Quaternion.Euler(0, $$anonymous$$athf.Lerp($$anonymous$$,max,lerpFactor), 0);

avatar image Vicas · Feb 20, 2013 at 08:27 AM 0
Share

If I do this:

 needleHub.transform.localRotation = Quaternion.Euler(0, 0, $$anonymous$$athf.LerpAngle(90f, -90f, lerpFactor));

It still goes the wrong direction. So how do I change direction? Substituing -90 with 270 did the same.

avatar image Loius · Feb 20, 2013 at 10:35 AM 0
Share

I wrote out "$$anonymous$$athf.Lerp" because that is the right function to use. Don't use LerpAngle, it lerps between angles which is just like Slerp. Use $$anonymous$$athf.Lerp.

avatar image Vicas · Feb 20, 2013 at 11:29 AM 0
Share

You are right. I should have done that :-) $$anonymous$$athf.Lerp worked.

Please convert your comment to an to answer if you would.

Show more comments
avatar image
1

Answer by mikesmullin · Apr 13, 2015 at 01:09 PM

Another way to do this is to Lerp from 89 to -89 degrees instead of 90 to -90. Since Lerp is "wired to take the shortest route", you are indicating which direction (clockwise or counter-clockwise) you intend to rotate by making it the shorter of the two paths.

This way may be less accurate, but you can mitigate that problem by making the difference much smaller like 89.999. I suppose this problem only happens when you are moving a difference of exactly 180.0 degrees, and don't like the default direction it will choose.

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

11 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

Related Questions

How to represent a gameobject's (x,y,z) rotation as a quaternion? 2 Answers

How to smoothly rotate to certain directions using input axis 1 Answer

Quaternion Rotation On Specific Axis Issue 0 Answers

Lerp rotation 90 degrees on jump 0 Answers

Changing rotation of Quaternion 0 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