• 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 /
This question was closed Mar 05, 2017 at 07:03 PM by alexanderameye.
avatar image
Question by alexanderameye · Mar 05, 2017 at 03:10 PM · rotationquaternionrotate objectslerprotate-around

Best rotation function to use

What would be the best rotation function (Quaternion.Lerp, Transform.Rotate...) to rotate an object around it's center axis. The rotation needs to be smooth, there needs to be an option to set the speed, and most important of all, for example if I want it to rotate from 0 degrees to -225 degrees, I don't want the object to choose the shortest rotation and rotate 135 degrees counterclockwise, but I need it to rotate 225 degrees clockwise until it reaches -220 degrees.

I'm a bit lost with all the functions Unity offers and it would be awesome if some of you guys could help me out!

Comment

People who like this

0 Show 5
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 Glurth · Mar 05, 2017 at 03:27 PM 0
Share

Want to help, but not into the "write it for me" Q's.

I'll suggest you take a look at the docs for all the members of the Quaternion class, in particular Euler (converts the Quaternion to a number of decrees around each axis), and Lerp (use this one to animate the rotation, fractionally, towards another rotation), or operator * (which is how you ADD Quaternion rotations together) ( suggest using Lerp OR operator *, not both)

Also take a look at Transform.rotation, this member contains the Quaternion that defines the orientation of the object in world-space and on screen. You'll want to change this value, over time, in order to animate the object.

Lastly, you will need to use Time.deltaTime to compute the amount of rotation to apply in a given frame, based on rotation speed you select.

Hope that helps clear it up a bit!

avatar image alexanderameye Glurth · Mar 05, 2017 at 03:41 PM 0
Share

I'm not asking you to write code for me at all. I'm just asking what you guys think would be the best function to achieve my goal.

For the Quaternion.Euler, do you mean I should use a a Lerp function nested in a public static Quaternion Euler(float x, float y, float z); ? I tried using Quaternion.Euler but I deemed it not useful because for example:

 hinge.transform.rotation = Quaternion.Euler(0,30,0);

didn't give me a smooth interpolation. Am I using this function wrong then?

Thank you for your answer!

avatar image Glurth alexanderameye · Mar 05, 2017 at 04:07 PM 0
Share

"I'm just asking what you guys think would be the best function to achieve my goal." cool :)

I would suggest a very limited use of Euler: in particular just when specifying your "Offset rotation" (this is the one that kind-of needs to be euler angles, since a human specifies it). Once it's a quaternion, you can use operator * to add it to your "Initial" rotation (transform.rotation), to get the "final" rotation.

I would store the "Initial" and computed "Final" rotations as Quatenions, and use Quatenion.Lerp to interpolate between them.

Show more comments

1 Reply

  • Sort: 
avatar image

Answer by AurimasBlazulionis · Mar 05, 2017 at 04:43 PM

You have a few options.

1) Multiply the rotation by the specific quaternion. It would be really efficient in most cases, but would require you to do it in fixed update.

 private Quaternion _rotation;
     public float speed;
     private float _speed;
     public Quaternion rotation {
         get {
             if (_speed != speed) {
                 _speed = speed;
                 _rotation = Quaternion.Euler (0, speed * Time.fixedDeltaTime, 0);
             }
             return _rotation;
         }
     }
 
     void FixedUpdate () {
         transform.rotation = transform.rotation * rotation;
     }

Or if you want to do it in Update, then you will need to call Quaternion.Euler every frame (w$$anonymous$$ch is not that good. You will only need speed variable. That is it.

 void Update () {
         transform.rotation = transform.rotation * Quaternion.Euler(0, speed * Time.deltaTime, 0);
     }

Or you can use Quaternion.LerpUnclamped. It is really cool. The code would look like t$$anonymous$$s:

     private Quaternion _rotation;
     public float rot;
     private float _rot;
     private Quaternion startRotation;
     private float startTime;
     public Quaternion rotation {
         get {
             return _rotation;
         }
     }
 
     void Update () {
 
         if (_rot != rot) {
             startTime = Time.time;
             _rot = rot;
             startRotation = transform.rotation;
             _rotation = Quaternion.Euler (0, transform.rotation.eulerAngles.y + rot / 3f, 0);
         }
 
         if (Time.time - startTime <= 1f)
             transform.rotation = Quaternion.LerpUnclamped (startRotation, rotation, (Time.time - startTime) * 3f);
     }

Sure you will need to change it. What it does, is when the target rotation changes, it creates a rotation Quaternion of the target rotation and divides the y target rotation by 3 so no the closes path is chosen.

Actually, other than these t$$anonymous$$ngs (not even sure about the last one), Transform.rotate would work the best and simplest.

Comment
alexanderameye

People who like this

1 Show 3 · 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 alexanderameye · Mar 05, 2017 at 05:38 PM 0
Share

with transform.rotate, can I set it to rotate for a specific amount of degrees and then stop?

avatar image AurimasBlazulionis alexanderameye · Mar 05, 2017 at 06:53 PM 0
Share

you can rotate for the time you want. That is basically Until angle / speed seconds pass. So it would be if (Time.time - startTime <= (angle / speed)) transform.Rotate(blah blah blah); where startTime is when you started to rotate.

avatar image alexanderameye AurimasBlazulionis · Mar 05, 2017 at 06:55 PM 0
Share

Thanks!

I also found someone that wrote a custom Quaternion.Slerp that bypasses the shortest-rotation problem and it works :)

http://answers.unity3d.com/questions/478617/not-interpolate-quaternion-on-shortest-path.html#answer-1304777

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

78 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

Related Questions

how to limit rotation of on object? (Quaternion.Slerp) 0 Answers

Change rotation of an object based on an HTC Vive Controller 1 Answer

Basic AI Locked Axis 1 Answer

Rigidbody2d Collision Breaks Quaternion.Slerp 2 Answers

Use Lerp to rotate object 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