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 Addexio3 · Sep 29, 2014 at 12:39 AM · day

Problem with sync (DayNightCycle)

Hey.

I got a little problem syncing things in my game. I have made a simple timer that displays hours, minutes and seconds on screen.

 //Controls the speed of time
 var timeSpeed : float;
 var timeSinceStart : float;
 
 function Update(){
     counter = (Time.time * timeSpeed)-timeSinceStart;
     
     if(counter >= 60){
         ResetCounter();
     }
     
     if(displayMinute == 60){
         displayMinute = 0;
         displayHour++;
     }
     if(displayHour == 24){
         displayHour = 0;
     }
 }
 
 function ResetCounter(){
     timeSinceStart = Time.time * timeSpeed;
     displayMinute++;
 }


What I am trying to do is add a sun thats rotating around the world in sync with the timer I made. How would I do that?

I been trying and failing so many times now. When I use this code the sun is allways faster then my timer, and it seems to get worse the more I increase the "timeSpeed".

 sun.Rotate(new Vector3(360,0,0)*Time.deltaTime*timeSys.timeSpeed);

Is there a way I could have a sun in sync with my timer?

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

Answer by Addyarb · Sep 29, 2014 at 02:38 AM

Use a good old fashioned stopwatch to time it. If it's moving at a whole number for speed, it should equal out to a whole number of seconds to do a complete revolution. Obviously humans aren't perfect at pressing start and stop, but just round it out to what seems to be the reasonable number.

What's the script you have on your sun? Is it rotating around your plane/terrain?

Comment
Add comment · 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 Kiwasi · Sep 29, 2014 at 03:28 AM 0
Share

You will always get small errors using this method, based on the precision of floats. The errors will accumulate over time, until the deviation is noticeable.

This is also why increasing the timeSpeed makes the problem worse.

The phenomenon is called floating point precision error. Worth Googling at some point

avatar image Addyarb · Sep 29, 2014 at 03:57 AM 0
Share

Hence why I stated that your speed must be a whole number, and not at a floating point.

avatar image Kiwasi · Sep 29, 2014 at 06:29 AM 0
Share

You could achieve exact precision using this solution and an int. But that's some fairly big jumps if you restrict yourself to moving your sun around by ints. You also must set the transform position directly, you can't add to the current position.

As long as you move a transform additively every frame floating point precision is unavoidable. Regardless of weather you work in floats or ints or 64-bit doubles. Unity still does transforms using floats. Transform.Translate and Transform.Rotate will not work for this application.

avatar image
0

Answer by Kiwasi · Sep 29, 2014 at 03:24 AM

What you are after is deterministic physics for your sun. Essentially you set the position of your sun based on the value of time.

First step is to get your time system working right. This is C#, you will have to find someone else to do the conversion, or convert yourself. Untested code, there might be typos.

 float currentTime;
 float timeSpeed;
 float startTime;
 
 void Update () {
     currentTime = (Time.time - startTime) * timeSpeed;
 }
 
 public int seconds {
     get {
         return (int)(currentTime % 60);
     }
 }
 
 public int minutes {
     get {
         return (int)((currentTime/60) % 60);
     }
 }
 
 public int hours {
     get {
         return (int)((currentTime/3600) % 24);
     }
 }
 
 public int days {
     get {
         return (int)((currentTime/86400));
     }
 }
 
 public float sunPosition {
     get {
         return (currentTime % 86400)/86400*360-180;
     }
 }

Note with the new set up you only need to do one calculation every frame. The other calculations are delayed until you actually call the various properties.

If you Update the position of your sun to match the angle given in sunPosition then your sun will always sync with time. You could even set a time in the future and see where the sun will be.

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 Addexio3 · Sep 29, 2014 at 01:46 PM 0
Share

I still use Time.deltaTime on my sun rotating tho, right?

I mean, if not wont the sun skip across the sky?

avatar image Kiwasi · Sep 29, 2014 at 08:22 PM 0
Share

You don't use Time.deltaTime. That's kind of the whole point of deter$$anonymous$$istic physics. Every time you increment a position or rotation using Time.deltaTime you introduce floating point precision errors. Do that often enough and you will be out of sync. Ins$$anonymous$$d you calculate the exact position each frame.

You use something like

  sun.eulerAngeles = new Vector3(timeSys.sunPosition,0,0);

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Day/Night Skybox 1 Answer

Day Night Cycle sun fade 1 Answer

day/night cycle runs only twice 1 Answer

Timed event Question 2 Answers

Water texture blending 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