Hey all,
I’ve been working on a 2d day/night cycle for about a week now, but I’ve hit a roadblock. I’ve been trying to get the light to turn different shades in sync with the in-game clock I have working, but no matter what I do, it will not do what I want it to. I’ve searched through the Unity Answers section for hours and hours, and I’ve about exhausted every thread I’ve found. The light either just lerps through morning to midday and then stops, or it flickers through at rapid speed, or just pops in the third color in the array, or something weird like that.
I want to find a way to sync them so that the coloring of the light will always match what time the clock says, even when I speed up the cycle for debug purposes. It would also help to be able to jump it forward, like if a cutscene plays out and takes one in game hour.
I’m still somewhat new to Unity, so any help I can get would be greatly appreciated.
This is the code I have to the clock:
public class DayNightCycle : MonoBehaviour {
private int dayLength;
private int morningStart;
private int noonStart;
private int eveningStart;
private int nightStart;
private int midnight;
private int currentTime;
public Color morningShade;
public Color noonShade;
public Color eveningShade;
public Color nightShade;
public float cycleSpeed;
private bool isDay;
private Vector3 sunPos;
public Light sun;
public GameObject earth;
// Use this for initialization
void Start () {
dayLength = 1440; //in-game minutes
morningStart = 360; //6am
noonStart = 720; //12pm
eveningStart = 1080; //6pm
nightStart = 1200; //8pm
midnight = 1440; //12am
currentTime = 360;
StartCoroutine (TimeofDay ());
earth = gameObject.transform.parent.gameObject;
}
// Update is called once per frame
void Update () {
if (currentTime > 0 && currentTime < morningStart) {
isDay = false;
} else if (currentTime >= morningStart && currentTime < noonStart) {
//isDay = true;
} else if (currentTime >= nightStart && currentTime < dayLength) {
isDay = false;
} else if (currentTime >= dayLength) {
currentTime = 0;
}
float currentTimeF = currentTime;
float dayLengthF = dayLength;
earth.transform.eulerAngles = new Vector3 (0, 0, (-(currentTimeF / dayLengthF) * 360) + 90);
sun.color =
}
IEnumerator TimeofDay(){
while (true) {
currentTime += 1;
int hours = Mathf.RoundToInt (currentTime / 60);
int minutes = currentTime % 60;
Debug.Log (hours + ":" + minutes);
yield return new WaitForSeconds (1F / cycleSpeed);
}
}
}