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
0
Question by CAPTNCAPS · Oct 23, 2014 at 03:25 PM · rotation

Why does transform.Rotate behave so strange?

Hello,

I hope you can help me understand how transform.Rotate works. (I have already checked the Documentation, didn't help...)

I have a light and wrote a script for it to make it rotate, to achive a Day-Night-Cycle. Now I want it to rotate faster at Night, so night is shorter. To do that I wanted to implement a if check to rotate it faster after a certain degree.

I wanted to find out when I need to rotate it faster, so I went ingame and watched the rotation Values. Here comes the first strange thing:

Rotation Value for X is increasing until 90°, stops at 90° for a second and then is getting smaller again, but now Y and Z are rotated 180° ; it rotates unitl 270° and then gets bigger again and Y and Z are 0° again.

So I setup my script to rotate faster when Sunlight.transform.rotation.x > 270

That didn't work. So I logged the rotation of the Sunlight. The values basically make a Sine from -1 to 1.

Why is it behaving like that? Why aren't the rotation values just increasing until 360° and then jump back 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 Baste · Oct 23, 2014 at 03:35 PM

The issue that you're running into is due to the fact that Unity uses something called Quaternions internally to represent rotations, instead of a 3-dimensional vector. A quaternion has 4 coordinates, uses complex numbers, and is pretty difficult to understand.

To do what you want to do, check the Euler Angles of the transform's rotation. so instead of:

 Sunlight.transform.rotation.x > 270

do this:

 Sunlight.transform.rotation.eulerAngles.x > 270

Hope that helps.

Comment
Add comment · Show 2 · 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 CAPTNCAPS · Oct 23, 2014 at 03:46 PM 0
Share

Ok thanks, I will check that out! :)

Edit: Cool! Worked just like I wanted! Thanks!

avatar image Baste · Oct 23, 2014 at 04:06 PM 0
Share

Can you mark the answer as accepted? That'll remove the question from the queue of unanswered questions.

avatar image
1

Answer by robertbu · Oct 23, 2014 at 03:32 PM

Read my answer here:

http://answers.unity3d.com/questions/799824/oafatwhy-does-transformrotationx-45-not-work.html

In the absence of your code, it is difficult to give specifics. If you are only rotating around one axis, then my suggestion is to keep track of the total rotation. That is treat the eulerAngles as write-only and track your own value:

 if (angles.x > 180 && angles.x < 360) {
      s = speedFast;
 } 
 else {
      s = speedSlow;
 }

 angles.x += s * Time.deltaTime;
 angles.x = angles.x % 360.0;
 transform.eulerAngles = angles;

'angles' is a Vector3 initialized to the starting angles of your object.

Note that unlike Rotate(), assigning to eulerAngles is a world rotation, but since you are only rotating on one axis, this will not make a difference.

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 Bunny83 · Oct 23, 2014 at 03:44 PM 0
Share

Uhh, having a seperate value and doing incremental changes to both is not a good ides. Since the rotation of the Gameobject is stored as Quaternion the two "angles" might drift slowly. You should set a fix rotation based on your angle. This has several advantages, especially when you want to set the "current time".

avatar image CAPTNCAPS · Oct 23, 2014 at 03:44 PM 0
Share

Hmmm... I still don't fully understand why it does what it does...

But your idea of solving the problem is great! Thanks!

Edit: Bunny83 how would I do that? I don't understand what you mean right now...

avatar image robertbu · Oct 23, 2014 at 03:50 PM 0
Share

@Bunny83 - changed the answer.

@CAPNCAPS - Do this:

  transform.eulerAngles = Vector3(180,0,0);
  Debug.Log(transform.eulerAngles);

You will see an output of (0,180,180)...the same physical rotation represented differently. See how your code checking the 'x' component would then fail?

avatar image CAPTNCAPS · Oct 23, 2014 at 04:13 PM 0
Share

Yes, I see. Strangly with this code, the object just stops rotation at 90°

avatar image Bunny83 · Oct 23, 2014 at 04:58 PM 0
Share

@CAPTNCAPS:

That is called a Gimbal Lock. This happens when a gimbal system (like the eulerangles) rotates the 2nd gimbal by 90°. You will effectively loose one degree of freedom because both the y and z axis are now identical.

Quaternions don't have this problem and that's why they are actually used. However Quaternions can't be viewed / edited in a human-readable form, that's why Unity converts the quaternion into euler angles, where we have again our gimbal-lock problem.

That's why when unity converts a rotation that points upwards into eulerangles you can get different results since eulerangles can represent certain rotations in multiple ways. The euler angles representation is not unique. That's why reading the euler angles is not recommended for such things.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Problem in rotation of plane 1 Answer

Change Pivot Point- Maya 3 Answers

Twin Stick Shooter controller 0 Answers

Controlling a rotating 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