• 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 imarriedyoung · Oct 09, 2013 at 03:43 AM · rotate objectquaternions

Quaternions, Eulers, and Small Float Values?

I have this itty bit of code…

 using UnityEngine;
 using System.Collections;
 
 public class PlanetManager : MonoBehaviour {
 
     void FixedUpdate()
     {
         rotateSun();
     }
     
     float degreesRotation;
     
     //rotate sun
     void rotateSun()
     {
         //Y degrees the ratio between every angle to every second in a day
         degreesRotation = 360.0f / ( Time.deltaTime * GameTime.speedOfDay * 86400.0f ); //0.004166667
         
         //rotate the sun by Y degrees for every second that passes
         print (degreesRotation);
         Quaternion rotation = Quaternion.AngleAxis( degreesRotation, Vector3.right ); 
         transform.rotation *= rotation;
 
 //        transform.Rotate(Vector3.right * degreesRotation);
         
 //        print(transform.rotation);
 //        print(rotation);
 
     }
 }

My issue lays in the fact that what degreesRotation spits out seems too small a value for any effect on the rotation - the object stays at it's same angle and doesn't move. If I test with a magic number like 0.05f it works, but if I add one more decimal point like 0.005f I seem to get nothing again. I've tested this with a few alternatives, as you can see, some in Quaternion (like Quaternion.Euler) since gimbal lock is a fear, and some with transform.Rotate / adding new vectors bit by bit.

Is my math wrong? or is this simply a 'thing' with these functions?

Comment
Add comment · Show 14
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 robertbu · Oct 09, 2013 at 04:36 AM 0
Share

I cannot create a simple reproduction of your issue. Are you sure that the rotations you are entering are big enough to have a visual effect frame to frame? I thought there my be some sort of rounding going on, so I tried this bit of code:

 #pragma strict
 
 private var rot = 0.000005;
 
 function Update() {
     for (var i = 0; i < 10000; i++) {
         //transform.Rotate(0.0, rot, 0.0);
         transform.rotation = Quaternion.AngleAxis( rot, Vector3.up) * transform.rotation; 
         }
 }

It applies a very small rotation 10,000 times per frame. I tried with both both the comment out line as well as the AngleAxis() line. Both worked fine. Pushing to 100,000 iterations with a 1/10 smaller value had the frame rate dramatically dropping, but the object did continue to rotate. So entering small values in the routines does not appear to be your issue.

avatar image robertbu · Oct 09, 2013 at 04:48 AM 0
Share

Took a second look. I don't know the value of GameTime.speedOfDay, nor do I know what 86400 represents, but if the whole line sets 'degreesRotation' to 0.0042 as suggested by the comment, then the object will rotate a about a degree every four seconds (when running on average at 60 FPS). So I tried it with:

 #pragma strict
 
 private var rot = 0.004;
 
 function Update() {
         transform.rotation = Quaternion.AngleAxis( rot, Vector3.up) * transform.rotation; 
 }

And I got an about one degree every four seconds rotation.

avatar image imarriedyoung · Oct 09, 2013 at 01:08 PM 0
Share

Tried that, changed Vector3.up to Vector3.right (so X axis ins$$anonymous$$d of Y) and it still won't move, however if I have it with your setup, on the Y axis, it will. The current light this is applied to is at -90 degrees on the X axis. If I change it to zero, for example, it does work, so it sounds like gimbal lock, but with a Quaternion?

Also, that would mean my math is definitely wrong. I'm trying to get this light to move N amount of angles per 1 second and smoothed out accordingly (via Time.deltaTime). Of course, to GameTime one second is not a constant 1 but ins$$anonymous$$d a the result of this expression

 seconds += speedOfDay * Time.deltaTime;

With a clause to reset after it has been 86400 seconds, or 1 day. The system works great, and it pairs up exactly with a stopwatch outside of the system, but that leaves me with trying to figure how how to make the sun move in accordance to time. Any ideas? Thanks for your time and words.

avatar image robertbu · Oct 09, 2013 at 02:36 PM 0
Share

Is this just a local vs. world axis issue? What happens if you use transform.Rotate() ins$$anonymous$$d of Quaternion.AngleAxis()?

avatar image ArkaneX · Oct 09, 2013 at 03:17 PM 0
Share

@imarriedyoung - why do you use FixedUpdate and not Update?

avatar image imarriedyoung · Oct 09, 2013 at 03:17 PM 0
Share

Same effect…

 degreesRotation = 360.0f / 86400.0f; //0.004166667
 transform.Rotate(new Vector3(degreesRotation, 0, 0) * Time.deltaTime, Space.World);

Still keeps 270 (-90) locked, as well as Space.Self, and left blank.

avatar image imarriedyoung · Oct 09, 2013 at 03:21 PM 0
Share

@Arkenex, no improvement, but I could see how that would work. I initially had GameTime calculate seconds in FixedUpdate to be independent of frame rate issues. I have tried setting them now both to FixedUpdate again, but it stays stuck. I'm curious what I'm doing wrong, I'll keep testing. alt text

screen shot 2013-10-09 at 11.20.18 am.jpg (116.9 kB)
avatar image ArkaneX · Oct 09, 2013 at 03:31 PM 0
Share

I think I know where your problem might be :)

When you check for actual rotation, you probably use

 print(transform.rotation);
 print(transform.eulerAngles);

By default this outputs values with 1 decimal place, but your rotation steps are too small to be displayed. Please change the number of decimal places by using:

 print(transform.rotation.ToString("F5"));
 print(transform.eulerAngles.ToString("F5"));
avatar image robertbu · Oct 09, 2013 at 03:32 PM 0
Share

It sounds like gimble lock, but I don't see how. What happens if you make the light a child of am empty game object, with the rotation done on the light and the script goes on the empty parent?

avatar image ArkaneX · Oct 09, 2013 at 03:36 PM 0
Share

Oops - I haven't seen the image before... So it looks you check rotation in inspector, and my previous comment doesn't apply :/

avatar image imarriedyoung · Oct 09, 2013 at 03:43 PM 0
Share

Yeah, it's confusing. If I change the angle to 271 or 269, it clamps to 270, if I check it to 280, it will move.

It is good to use those print's though, so I've included them and…

I just tried inputting 0.4166667f into the Quaternion statement and it will move, both outputs move marginally (Quaternion and Euler). If I input the variable I have with it's expression, however, I get no movement from the Euler outputs in console but I do get Quaternion movement.

I have verified no other scripts are causing this by creating a new sun object from scratch and turning off all other previous scripts so there are no conflicts. $$anonymous$$aybe I should try testing this in another project, too.

avatar image imarriedyoung · Oct 09, 2013 at 04:03 PM 0
Share

@robertbu no such luck there, either. alt text

screen shot 2013-10-09 at 12.03.10 pm.jpg (131.1 kB)
avatar image imarriedyoung · Oct 09, 2013 at 04:19 PM 0
Share

I think it really is just a matter of the limitation of Euler's v Quaternions and not gimbal lock. I ran a test right now, noticing that the Quaternions were moving and not the Eulers. I am still fiddling with the math and found that the sun was moving too fast in one calculation (a whol day should equal 86400 seconds, which may seem absurd but the multiplier will change that to anything I want). I changed it again to a magic number consisting of 1 / 360 (one second divided by total angles) and albeit the movement is really slow, after a solid few 'game $$anonymous$$utes '(using Time.timeScale to fast forward) the Euler angle did eventually start to nudge. Thanks for all your help and suggestions, guys.

avatar image ArkaneX · Oct 09, 2013 at 04:36 PM 0
Share

I just finished another test using your code:

 degreesRotation = 360.0f / 86400.0f; //0.004166667
 transform.Rotate(new Vector3(degreesRotation, 0, 0) * Time.deltaTime, Space.World);

I set up sample scene - camera, cube and dir light. Attached Planet$$anonymous$$anager (with above two lines) to the cube. I then set:

cube position to 0,0,0 cube rotation to 270,0,0 cube scale to 1,500,1

camera location to 10,0,250 camera rotation to 0,270,0

After this, I'm able to observe end of quite a long object. After pressing Play, I can see that this end is slowly rotating, but X rotation in inspector stays at 270. So this must be some problem with Euler angles indeed, just as you observed.

0 Replies

· Add your reply
  • Sort: 

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.

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

16 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

Related Questions

Rotating an object in relation to its endpoints 1 Answer

How to steer a vehicle 1 Answer

How can I make a character look in a direction that's halfway between its direction of 2D movement and facing the camera? 1 Answer

Rotating 2D sprite to match surface. 0 Answers

Quaternion reset rotations didnt smooth 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