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 SideKick · Feb 17, 2015 at 08:03 AM · c#timerrotate object

Orbit over 1 hour with acurate timer

I'm trying to create a 2D day & night cycle with a sun orbiting a planet around the z axis taking an hour to fully rotate. I need a timer to capture the hours / days past since the game started which syncs perfectly with the current orbit of the sun.

So far I have created the rotation and it works pretty well, however my timer goes off track by the second orbit. Is there a way to sync my hour, day floats with the rotation of the sun?

Cheers

     private float rotateTime = 1.0f; // 1 hour.
     private float degressToRotate;
 
     public float hours;
     public float days;
 
     void Start() 
     {
         InvokeRepeating("AddHours", 150.0f, 150.0F);
     }
 
     
     void Update () 
     {
         degressToRotate = (360/(rotateTime*60*60)) * Time.deltaTime;
         gameObject.transform.Rotate(0f, 0f, degressToRotate);
     }
 
     void AddHours()
     {
         if(hours >= 24f) 
         {
             hours = 0f;
             gameObject.transform.Rotate(0f, 0f, 0f);
             days++;
         }
         else {
             hours++;
         }
     }
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

1 Reply

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

Answer by Baste · Feb 17, 2015 at 08:44 AM

Rotation, hour and day are not values you should be setting separately - they'll get out of sync eventually, no matter how accurate you are with how you try to sync them.

Instead, read of Time.time, and use that to set each of the other values. This example uses a public float field that gives the length of day, and uses Gizmos to show that the correct day and hour is being set.

Note that this will have to be attached to the point of rotation, and whatever's rotating needs to be a child object of that. That's what you're already doing, but just to be safe.

 public float dayLength;
 float rotationSpeed;
 
 void Start()
 {
     rotationSpeed = (1 / dayLength) * 360f;
 }
 
 // Update is called once per frame
 void Update()
 {
     transform.eulerAngles = new Vector3(0, 0, Time.time * rotationSpeed);
 }
 
 private int GetHour()
 {
     return (int)((Time.time / (dayLength / 24)) % 24);
 }
 
 private int GetDay()
 {
     return (int)(Time.time / dayLength);
 }
 
 void OnDrawGizmos()
 {
     if (Application.isPlaying)
         UnityEditor.Handles.Label(transform.position, "Day: " + GetDay() + " Hour: " + GetHour() + " Real time: " + Time.time);
 }
Comment
Add comment · Show 4 · 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 SideKick · Feb 17, 2015 at 08:55 AM 0
Share

thanks for that much appreciated, I would like to test it out but I getting the following error.

 transform.rotation assign attempt for 'Sun' is not valid. Input rotation is { NaN, NaN, NaN, NaN }.
 UnityEngine.Transform:set_eulerAngles(Vector3)
avatar image Kiwasi · Feb 17, 2015 at 10:24 AM 0
Share

This solution will still get out of sync after several days. You really want a fully deter$$anonymous$$istic system, where you calculate the position/rotation of the sun each frame based on the time, rather then incremental rotation that will accumulate rounding errors.

avatar image Baste · Feb 17, 2015 at 11:36 AM 0
Share

@Side$$anonymous$$ick, you'll get that error if you set the day length to 0 in the inspector. Dividing by 0 isn't usually a good idea.

@Bored$$anonymous$$ormon, that's exactly what I'm doing. Was that comment meant for the OP post, or?

avatar image SideKick · Feb 17, 2015 at 12:38 PM 0
Share

oops yeah, thanks works perfectly now :) 88 in-game days and in perfect sync.

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

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Making a Timer Out out of 3D Text using C#. 1 Answer

Start timer on mouse0 click 1 Answer

How to make UI image in screen space look at a gameobject in world space? 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