Hello Community,
Ill just cut to the chase. I am a little busy right now.I have a problem with my code. I am trying to make a Day and Night cycle by just changing the background of the camera. I have never done this before so that is probably why this does not work. But anyways the problem that is happening is that the colors do not blend together like they do in the unity example. And that is what I am trying to do. It still changes the color but only when it hits the certain time and it does not blend.
Here is my code
using UnityEngine;
using System.Collections;
//This script is attached to the GameManager and it controls the time of the game and the day/night
public class DayNightCycle : MonoBehaviour
{
//GameObjects
public GameObject TimeOfDayText;
public GameObject PlayerCamera;
//Colors
public Color color1 = Color.black;
public Color color2 = Color.blue;
public Color color3 = Color.blue;//This is a lighter blue
public Color color4 = Color.blue;//This is a darker blue
//Time
private float MinTime = 0f;
private float MaxTime = 2400f;
private float CurrentTime;
public float Duration1 = 3;
public float Duration2 = 3;
public float Duration3 = 3;
public float Duration4 = 3;
//Time of Day Times
public float DawnTime = 600f;
private float NoonTime = 1200f;
private float DuskTime = 2000f;
private float MidnightTime = 2400f;
//Bool for Time of day
public bool Dawn;
public bool Noon;
public bool Dusk;
public bool Midnight;
// Use this for initialization
void Start ()
{
StartCoroutine(GameTimer());
}
// Update is called once per frame
void Update ()
{
if(Dawn)
{
CurrentTime = DawnTime;
Dawn = false;
}
if(Noon)
{
CurrentTime = NoonTime;
Noon = false;
}
if(Dusk)
{
CurrentTime = DuskTime;
Dusk = false;
}
if(Midnight)
{
CurrentTime = MidnightTime;
Midnight = false;
}
AddjustCurrentTime(0);
if(CurrentTime > MidnightTime && CurrentTime <=DawnTime)//Transition from Midnight to Dawn
{
float time = Mathf.PingPong(Time.time, Duration1) / Duration1;
PlayerCamera.camera.backgroundColor = Color.Lerp(color1, color2, time);
}
if(CurrentTime > DawnTime && CurrentTime <= NoonTime)//Transition from Dawn to Noon
{
float time = Mathf.PingPong(Time.time, Duration2) / Duration2;
PlayerCamera.camera.backgroundColor = Color.Lerp(color2, color3, time);
}
if(CurrentTime > NoonTime && CurrentTime <= DuskTime)//Transition from Noon to Dusk
{
float time = Mathf.PingPong(Time.time, Duration3) / Duration3;
PlayerCamera.camera.backgroundColor = Color.Lerp(color3, color4, time);
}
if(CurrentTime > DuskTime && CurrentTime <= MidnightTime)//Transition from Dusk to Midnight
{
float time = Mathf.PingPong(Time.time, Duration4) / Duration4;
PlayerCamera.camera.backgroundColor = Color.Lerp(color4, color1, time);
}
}
void OnGUI()
{
TimeOfDayText.GetComponent<TextMesh>().text = "It is " + CurrentTime;
}
void CameraFlags()
{
camera.clearFlags = CameraClearFlags.SolidColor;
}
public void AddjustCurrentTime(int adj)
{
CurrentTime += adj;
if(CurrentTime < MinTime)
{
CurrentTime = MinTime;
}
if(CurrentTime >= MaxTime)
{
CurrentTime = MinTime;
}
}
IEnumerator GameTimer()
{
while(true)
{
if(CurrentTime < MaxTime)
{
CurrentTime += 1f;
yield return new WaitForSeconds(1f);
}
else
{
yield return null;
}
}
}
}