Attaching Light lerp to in-game clock
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);
}
}
}
Answer by UnityCoach · Apr 07, 2017 at 07:37 PM
You could simplify this a bit using a Gradient instead.
public Gradient dayLight;
void Update ()
{
sun.color = dayLight.Evaluate (currentTime); // must be normalised between 0 and 1
}
Using a gradient did cross my $$anonymous$$d... But what do you mean normalize is between 1 and 0? And also, how would the gradient account for the use of 4 light colors? And would it be able to loop through the cycle properly?
The value you pass to Gradient.Evaluate must be between 0 and 1.
So, you'll have to figure out the math so that 0 = midnight, 0.25 = 6am, 0.5 = 12pm and 0.75 = 6pm.
You can then add as many colours to the gradient as you like. If you want to handle more than just one light, you could have several gradients, one for the sun, one for the ambient for example.
You could also use the alpha for the intensity, though it can't go passed 1, so I would rather use an AnimationCurve along for the intensity, as it works the same way as a Gradient.
Sorry if this is a little nocroposty. I'm still having issues. I see what you're suggesting, it's just implementing it now that's the problem. I can't get the gradient to increment. I believe the currentTime variable need to be somehow changed to accommodate a 0-1 scale. Is there a way to do that without messing everything up in the normal clock? Or do I need to create a whole new cycle that runs simultaneously to the clock, and how would those sync up?
I know. I'm full of a thousand questions here. :P
Your answer
Follow this Question
Related Questions
Hinge Joint motor not effecting joint 0 Answers
2d Light(mapping) alternative for mobile 0 Answers
Point light moves to origin 1 Answer
Using a Shader and Lighting Renders a Sprite's Outline When it is Transparent 1 Answer
Disable light not working 0 Answers