Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
3
Question by shadow11235 · May 23, 2013 at 05:49 PM · rotationangleeuleranglesslerplocalrotation

How to use Euler angles to rotate an object ?

I have an object that I need to rotate from its local rotation by an amount of degrees,on all 3 axis? I have never used euler angles before, but I know that I should be using it as a normal rotation will not work.. Lets say I want to rotate my object with an angle of Vector3(264,29,319) and it's current localRotation is (6,331,41) and the wanted final rotation of the object is (270,0,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

5 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by robertbu · May 23, 2013 at 10:26 PM

Here are my suggested "rules" for how you deal with eularAngles in this situation:

  • Never read eulerAngles. Consider them write-only.

  • Keep your own Vector3 that represents the current rotation.

  • Never assign to an individual eulerAngles component (like eulerAngles.y). Always assign a Vector3 to eulerAngles.

Here is a simple script:

 #pragma strict
  
 public var speed = 1.0;
 private var v3To = Vector3(2025,0,0);
 public var v3Current = Vector3(0,0,0);
  
 function Start() {
    transform.eulerAngles = v3Current; 
 }
  
 function Update () {
     v3Current = Vector3.Lerp(v3Current, v3To, Time.deltaTime * speed);
     transform.eulerAngles = v3Current; 
 }

Note changes in rotation are made to v3Current and then v3Current is assigned to Transform.eulerAngles. This way v3Current will always reflect one euler angle representation of the 'physical' rotation. But note that the actual eulerAngles value will not necessarily match v3Current. But they will both represent the same 'physical' rotation.

Quaternion.Lerp() will select the shortest distance between two rotations, so that will not work for you. Vector3.Lerp() will literally interpret the values passed. So if you want to rotate the long way from 41 to 0, you have to change the values so the Lerp() walks that way. In this example, you would set the final rotation to 360 rather than 0.

Note in playing these games, you typically want to normalize the v3Current values back into a standard range (0 to 360 or perhaps -180 to 180) before calculating and setting the new destination angle.

Comment
Add comment · Show 7 · 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 shadow11235 · May 24, 2013 at 04:12 PM 0
Share

Thanks, That is really helpful info. so what if I am trying to go from 290 to 270, will Vector3.lerp interpret it exactly the way I pass it?

Thanks!

avatar image robertbu · May 24, 2013 at 04:28 PM 1
Share

Going from 290 to 270 it will go the short way. If you want to go the long way, you will need to use 630 (270+360=630) as the destination. As the end of the rotation, your Vector3 will say 630, but eulerAngles will likely say -90. As mentioned above, before you use that Vector3 again, you will want to normalize your angles into a set range (0 - 360 more likely).

avatar image Talavang · Sep 20, 2013 at 07:11 PM 0
Share

you save my day !! thanks

avatar image CashaNova · Jul 02, 2014 at 10:11 PM 0
Share

This was very helpful. This is the first thing I've checked this forum for and it was exactly what I needed to know. Thanks!!!

avatar image WikiMalik · May 25, 2015 at 01:16 AM 0
Share

That for elaborate explanation. I've took it's reference thrice today.

Show more comments
avatar image
0

Answer by darthbator · May 23, 2013 at 05:55 PM

Why not use Quaternian.Lerp to LERP the rotation to the target?

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 shadow11235 · May 23, 2013 at 06:00 PM 0
Share

Wouldn't the LERP take the shortest way possible to the final angle and in the case mentioned above, The current z angle is 41 and my final z angle is 0, which means it will decrement the angle by 41 ins$$anonymous$$d of incrementing it by 319 which is what I want ?

avatar image
0

Answer by Stormizin · May 23, 2013 at 05:56 PM

Try: Quaternion.rotate(270,0,0);

See more: Euler

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 Zplintz · May 23, 2013 at 07:40 PM

I'm just a noob to Unity (and 3d gaming/modelling) but I think there are fundamental issues with using Euler maths (in general - not just Unity) to rotate objects if you rotate more than one axis at a time - I believe you should be using Quaternian maths instead.

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 koralturkk · Nov 02, 2020 at 08:48 PM 0
Share

The issue is the possibility of a Gimbal Lock which a case in certain orientation of rotations, you use a degree of freedom. You can search it on youtube, there are quite nice videos about it

avatar image
0

Answer by akeemovic · Aug 17, 2021 at 04:10 PM

I used this logic in Unity 2021 and it worked fine. I only needed to convert it to C#.

@robertbu
Thanks, very much.

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

21 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

Related Questions

Limit rotation angle on Z axis while slerp 1 Answer

rotate slowly! 1 Answer

Object makes random jumps in rotation 1 Answer

Quaternion lerp is not rotating my object to the desired rotation? 1 Answer

Head and body rotate threshold 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