- Home /
Unity2D Day and Night Cycle
Hi All,
I'm currently working on an individual project and I'm looking for the best way to implement a Day and Night Cycle into my game. So I'm going to give a brief description about my game:
It's a pixel 2D side-scroller survival sandbox RPG, similar to Terraria.
For my project, I want to add the feature of a Day and Night Cycle, to give the game a bit of an ambience, but I'm finding it increasingly difficult to implement it into a 2D game. I've searched through the forums for answers, but most, if not, all, are aimed at Unity3D projects. Telling you to change mesh colors and whatnot.
In my project, I've added Time, where 1 minute is 2 seconds real time. I'll post the code of my Time script below. In my Time script, I've implemented periods of the day, to help aid with the sky color.
using UnityEngine;
public class RelativeTime : MonoBehaviour
{
// Variables
TimeUI WorldTime;
readonly float TimeDelay = 2.5F;
int Hours, Minutes;
float Timer;
string HourValue, MinuteValue;
// Periods of the Day; in a 24-hour clock format
const int Midnight = 0; // 00:00 or 12:00 AM
const int Dawn = 4; // 04:00 or 04:00 AM
const int Sunrise = 6; // 06:00 or 06:00 AM
const int Morning = 7; // 07:00 or 07:00 AM
const int Afternoon = 12; // 12:00 or 12:00 PM
const int Evening = 17; // 17:00 or 05:00 PM
const int Sunset = 18; // 18:00 or 06:00 PM
const int Dusk = 20; // 20:00 or 08:00 PM
const int Night = 21; // 21:00 or 09:00 PM
// Max Minutes/Hours in a Day; also in a 24-hour clock format
readonly int MaxMinutes = 59;
readonly int MaxHours = 23;
string DayPeriod;
// Methods
void Awake()
{
WorldTime = GetComponent<TimeUI>();
/*
* This area will be used to load in saved game data
* e.g. time of day, time format, etc.
*/
#region Load Data
#endregion
}
void Start()
{
updateTime();
checkDayPeriod();
}
void LateUpdate()
{
Timer += Time.deltaTime;
if (Timer >= TimeDelay)
{
ResetTimer(0.0F);
IncreaseTime();
}
}
void ResetTimer(float value)
{
Timer = value;
}
void IncreaseTime()
{
Minutes++;
if (Minutes > MaxMinutes)
{
Minutes = 0;
Hours++;
checkDayPeriod();
if (Hours > MaxHours)
{
Hours = 0;
}
}
WorldTime.updateTime(getTime(WorldTime.getClockType()));
}
void updateTime()
{
WorldTime.updateTime(getTime(WorldTime.getClockType()));
}
void checkDayPeriod()
{
switch (Hours)
{
case Midnight:
this.DayPeriod = "Midnight";
break;
case Dawn:
this.DayPeriod = "Dawn";
break;
case Sunrise:
this.DayPeriod = "Sunrise";
break;
case Morning:
this.DayPeriod = "Morning";
break;
case Afternoon:
this.DayPeriod = "Afternoon";
break;
case Evening:
this.DayPeriod = "Evening";
break;
case Sunset:
this.DayPeriod = "Sunset";
break;
case Dusk:
this.DayPeriod = "Dusk";
break;
case Night:
this.DayPeriod = "Night";
break;
default:
break;
}
WorldTime.updateDayPeriod(DayPeriod);
}
public string getTime(bool twelveHour)
{
string timeOfDay, dayPeriod;
// Format the Minutes
if (Minutes < 10)
{
MinuteValue = "0" + Minutes.ToString();
}
else
{
MinuteValue = Minutes.ToString();
}
// Format the Hours
if (Hours < 10)
{
HourValue = "0" + Hours.ToString();
}
else
{
HourValue = Hours.ToString();
}
// Twelve-hour clock
if (twelveHour == true)
{
if (Hours > Afternoon)
{
// Single digits should be displayed as 01...09, not 1...9
if ((Hours % Afternoon) > 9)
{
HourValue = (Hours % Afternoon).ToString();
}
else
{
HourValue = "0" + (Hours % Afternoon).ToString();
}
}
else if (Hours.Equals(0))
{
HourValue = Afternoon.ToString();
}
// Check period of day, i.e. AM or PM
if (Hours >= Afternoon)
{
dayPeriod = "PM";
}
else
{
dayPeriod = "AM";
}
timeOfDay = HourValue + ":" + MinuteValue + " " + dayPeriod;
return timeOfDay;
}
else
{
timeOfDay = HourValue + ":" + MinuteValue;
return timeOfDay;
}
}
}
I guess that's where my main question comes into things: is there any way I could change a sprite color using C# script, in accordance with the periods of the day? I'm looking into Color.Lerp, but changing color gradually e.g. from Sunrise to Morning. So, from Sunrise, the sky goes from a black sky to a dark orange, gradually getting brighter as time increments, but also changes to a blue as Morning approaches. Is this at all possible?
Any answers are welcomed!
Your answer
Follow this Question
Related Questions
How to impliment a 10 minute day clock, and tie it to a directional light rotation. 4 Answers
Timed event Question 2 Answers
Time of day script not working 2 Answers
Is it possible to turn off/suppress event calls in an animation at runtime? 2 Answers
Parameterize the Animators transition condition (in 2D) 1 Answer