- Home /
Real-time day/night system breaks after noon
I've created a day/night system that basically rotates a directional light at a rate that would be realistic to the real life passage of time. It also calculates the position the "sun" should start at based on the current system time. This script was working beautifully this morning, and I tried it a few minutes before noon today and everything still worked.
After 12:00pm, when I run the game it thinks that it is night time. Not sure why, as the calculations used to decide where the sun should be is based on how many seconds have passed since 00:00 (midnight). For example, if the time is 12:27pm it takes the number of hours and figures out how many seconds that would be (12*60*60) and adds that to the number of minutes converted to seconds (27*60) then adds in the current number of seconds (say 54) and multiplies all that by the rate of rotation (0.004166667 degrees per second) then sets the rotation to that position.
That means that it should be set to (0,0,186.975014958) but it is instead setting to around 55.91.
Any ideas??
#pragma strict
public var normalPassageOfTime = true;
//How many degrees the sun rotates per second
private var degreesOfRotation : float = 360f /*degrees*/ / 24f /*hours*/ / 60f /*minutes*/ / 60f /*seconds*/;
//Visible variable to adjust speed of time flow
public var speedOfLight : float = 1f;
//Calculation of time flow speed
private var speedModifier : float;
//Get Hours, Minutes, and Seconds as a string
public var currentHour = System.DateTime.Now.ToString("HH");
public var currentMin = System.DateTime.Now.ToString("mm");
public var currentSec = System.DateTime.Now.ToString("ss");
//Convert current number of hours/minutes passed since midnight into usable seconds
//Seconds will just equal current as a float
private var hoursInSeconds : float = parseFloat(currentHour) * 60f * 60f;
private var minutesInSeconds : float = parseFloat(currentMin) * 60f;
private var secondsInSeconds : float = parseFloat(currentSec);
function Awake() {
//set position of the sun based on time of day
transform.Rotate(0,0,degreesOfRotation * (hoursInSeconds + minutesInSeconds + secondsInSeconds));
}
function Start () {
}
function Update () {
//rotate the sun based on set speed (Needs Variables adjusted)
if (normalPassageOfTime == true) {
transform.Rotate(0,0,speedModifier * Time.deltaTime);
//Multiplies speed variable by number of degrees to rotate per second
speedModifier = degreesOfRotation * speedOfLight;
}
}
Ladies and gentlemen, I've figured out my "Why". It seems that I had adjusted the rotation of the light between when it was working and when it stopped working. Apparently, the way I have it written, the algarithem just adds the number of degrees to the current rotation. Is there a way to normalize the rotation to (0,0,0) first, or to have it set the rotation to exactly what is in the script ins$$anonymous$$d of having it add on the degrees?
Thanks
Answer by alexi123454 · Jul 20, 2015 at 07:09 PM
So as you said in your comment, transform.Rotate will actually add a rotation of that many degrees around the axis, rather than just setting its rotation. There are a number of simple ways to get around that, the simplest of which is to set the object's rotation to Quaternion.identity before doing the transform.Rotate function. Another simple way is to add rotations, which can be done by multiplying the quaternions: transform.rotation = (Quaternion.Euler(0,0,0) * Quaternion.Euler(0,0,newRotation)) will give you a rotation of 0, and then a rotation around the z axis of newRotation.
$$anonymous$$an, That is super helpful! I haven't tried this yet, but I will later today. I don't know what the Quaternion business is, or how it works, but I'll just throw it in there and do a little research on it so I can be sure I understand how my own script functions.
Thank you!