I’m working on a day/night system and I’d like the transition time to be based off the daylength input. The whole system is in sync with the daylength speed so if the daylength speed is set at 10 it’s a 10 second day and so forth.
How can I have the timeOfDay transition according to the daylength speed? I need it to be consistently transitioned at whatever speed the days are. Maybe a transition time similar to Skyrim’s based off a 24 minute day, then if the speed is set to a 1 minute day then the transitions will be the same, but just sped up. Hopefully I explained it well enough.
How can I do this? Here’s some of what I’ve coded you can base it off that.
This is what I came up with. Sorry if it’s a bit huge, but I thought it might be useful to make it decent. I tested it and it seems to work fine. Let me know if it doesn’t work correctly.
using UnityEngine;
using System.Collections;
using System;
public class TimeOfDay : MonoBehaviour
{
public float timePerDay = 1140.0f; // In seconds
public float sunrise = 6.0f; // In hours
public float sunset = 17.0f; // In hours
public float hoursPerDay = 24.0f; // In hours
public Color dayAmbientColor;
public Color dayBackgroundColor;
public Color nightAmbientColor;
public Color nightBackgroundColor;
public float currentTime = 0.0f;
private float NightDuration
{
get { return timePerDay - RelativeSunset + RelativeSunrise; }
}
private float DayDuration
{
get { return RelativeSunset - RelativeSunrise; }
}
private float RelativeSunrise
{
get { return timePerDay * (sunrise / hoursPerDay); }
}
private float RelativeSunset
{
get { return timePerDay * (sunset / hoursPerDay); }
}
private float Midday
{
get { return (DayDuration * 0.5f) + RelativeSunrise; }
}
private float Midnight
{
get { return Mathf.PingPong((NightDuration * 0.5f) + RelativeSunset, timePerDay); }
}
// Use this for initialization
public void Start ()
{
}
// Update is called once per frame
public void Update ()
{
currentTime += Time.deltaTime;
// If we have reached the end of day start the cycle over
if (currentTime >= timePerDay)
currentTime = currentTime - timePerDay; // Keep the extra time
Color currentAmbientColor = Color.black;
Color currentBackgroundColor = Color.black;
float midday = Midday;
float midnight = Midnight;
float sampleTime = 0.0f;
float minutesPastMidnight = (currentTime > midnight && currentTime < timePerDay) ? timePerDay - currentTime : timePerDay - midnight + currentTime;
if (currentTime > midday && currentTime <= midnight)
{
sampleTime = (currentTime - midday) / DayDuration;
Debug.Log("Sample Day: " + sampleTime);
}
else if (minutesPastMidnight >= 0)
{
sampleTime = 1.0f - (minutesPastMidnight / NightDuration);
}
currentAmbientColor = Color.Lerp(dayAmbientColor, nightAmbientColor, sampleTime);
currentBackgroundColor = Color.Lerp(dayBackgroundColor, nightBackgroundColor, sampleTime);
RenderSettings.ambientLight = currentAmbientColor;
Camera.main.backgroundColor = currentBackgroundColor;
}
}