- Home /
Day/Night Cycle/Weather System
Hello all
Is there a good way to implement a working (simple!) day/night cycle into my game? I don't need anything complicated like a moon, just so that it gets dark for a minute or two, then automatically turns day again.
Also, is there a good way to make a weather system that randomizes weather between sun, rain, etc?
Thanks in advance!
Answer by Marco-944 · Nov 28, 2019 at 11:34 AM
You can make an IEnumerator Coroutine, which counts down the time of day/night and then sets a value to true.
Like this: You have a Light Object, that we will be referencing and a bool that tells us if it is day or not. This will probably not work if you copy paste it, but you can write it down and use the autocorrect function.
void Start()
{
StartCoroutine(DaylightCycle());
}
IEnumerator DaylightCycle()
{
isDay = false;
yield return new WaitForSeconds(please_insert_night_time_here);
isDay=true;
yield return new WaitForSeconds(please_insert_day_time_here);
StartCoroutine(DaylightCycle());
}
void Update()
{
//Here we will make the lights go out
if (isDay==false)
{
if (LightObject.GetComponent<Light>().intensity > 0)
{
LightObject.GetComponent<Light>().intensity -0.2f*Time.deltaTime;
}
}
if (isDay==true)
{
if (LightObject.GetComponent<Light>().intensity < 1)
{
LightObject.GetComponent<Light>().intensity +0.2f*Time.deltaTime;
}
}
}
This will probably throw a lot of errors, I just wrote it for you to explain how I would do it hope it helps
Your answer
![](https://koobas.hobune.stream/wayback/20220612215402im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sun Revolution... 1 Answer
fading between skyboxes 1 Answer
Weather - Snow and rain effecting objects 2 Answers
Single lightmap with day/night cycle 0 Answers
Creating Foggy Atmosphere 1 Answer