# 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?

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.

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?