- Home /
Repeatedly lerp through array for day night cycle
Hi everybody ! I recently switched from Java to C# and am currently working on a few assets I plan to use in a later project. Inspired by one of the unity stories I decided to try to write my own daynight cycle, which should be easily controllable through a custom editor. So far I got the basics down, finished the most important parts of my custom editor and rotated the sun 360 degrees once to test if I calculated my time (float 0-1) correctly.
However, to make it really user friendly I want my user to be able to define checkpoints that define sun position, brightness, cloud speed,... (which are all stored in serializeable TimeOfDay objects) at specific times. My DayCycler should then calculate all the values between them. And this is where I am stuck. Currently I have these two classes:
using UnityEngine;
using System.Collections;
using System;
public class DayCycler : MonoBehaviour {
#region object references & setup
public int dayDuration;
public Light sun;
public Material atmosphereGradient;
public FogVolume fogVolume;
public Material horizonClouds;
public Light moon;
#endregion
//used to change the day length by other scripts.(weather, magic?)
public float timeMultiplier=1.0f;
public TimeOfDay currentTimeOfDay;
public TimeOfDay[] timesOfDay;
// Use this for initialization
void Start () {
currentTimeOfDay = new TimeOfDay();
currentTimeOfDay.time = 0;
currentTimeOfDay.name = "Automatic";
}
// Update is called once per frame
void Update () {
UpdateCurrentTimeOfDay(currentTimeOfDay.time);
UpdateScene();
currentTimeOfDay.time += (Time.deltaTime / dayDuration) * timeMultiplier;
if (currentTimeOfDay.time >= 1) currentTimeOfDay.time = 0;
}
private void UpdateCurrentTimeOfDay(float time)
{
if (timesOfDay.Length <= 1) return;
//update all variables in currentTimeOfDay depending on the time
//HOW???
}
private void UpdateScene()
{
// TODO Write UpdateScene() once time of day is working
}
}
and:
using UnityEngine;
using System.Collections;
using System;
[Serializable]
public class TimeOfDay{
public String name;
public float time;
public Vector3 sunRotation;
public Color sunColor;
//and so on...
}
I considered to simply look for the two closest values to the current time in the array and then use their distance to the current time to calculate my values, but that would be kind of 'flat'. Has anyone any ideas how I could make this work?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Array performance on android device 1 Answer
Only instantiate GameObjects with a certain tag from an array 1 Answer