- Home /
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?
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?
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
Hence why I stated that your speed must be a whole number, and not at a floating point.
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.
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.
I still use Time.deltaTime on my sun rotating tho, right?
I mean, if not wont the sun skip across the sky?
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
Follow this Question
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