I am using a directional light with a sun flare attached to it and I want it to rotate to simulate day and night. I need it so that the intensity is is less in the mornings and afternoons and off at night.
Lets assume that time is a float that goes from 0 to 100. We can define its orbit by:
transform.position = new Vector3(OrbitRange * Mathf.Cos((time/100) * 2 * Mathf.PI), OrbitRange * Mathf.Sin((time/100) * 2 * Mathf.PI), 0);
So now, as the time increases, the light will move in a circular pattern.
Now for intensity. If we take time = 25 as midday, time = 75 as midnight, then we can set from 55 to 95 as night time, ie. no light. approx. 95 - 5 and 45 - 55 can be classified as the ‘twilight’ periods.
So:
if(time > 55 && time < 95){
light.intensity = 0;
}
else if(time > 95 || time < 5){
light.intensity = 0.5;
}
else if(time > 45 && time < 55){
light.intensity = 0.5;
}
else{
light.intensity = 1;
}
to keep the light pointing at the middle of the world, we throw in:
transform.LookAt(new Vector3(0,0,0));
and now we increment time
time += rateOfTime;
if(time > 100){
time = 0;
}
Throw all this into our Update() method, and VIOLA! rotating sun with changing intensity