I'm trying to make a day and night cycle that also keeps track of how many days passed. I watched a tutorial to start and for this post, I removed all the unnecessary stuff like rotating the sun and changing colors.
public class TimeController : MonoBehaviour
{
[SerializeField]
private float timeMultiplier;
[SerializeField]
private float startHour;
private DateTime currentTime;
void Start()
{
currentTime = DateTime.Now.Date + TimeSpan.FromHours(startHour);
}
void Update()
{
UpdateTimeOfDay();
}
private void UpdateTimeOfDay()
{
currentTime = currentTime.AddSeconds(Time.deltaTime * timeMultiplier);
}
}
The biggest issue I have with this is instead of timeMultiplier I'd rather have like a dayLength variable which represents how long a day should last in minutes so one day is 10minutes int dayLength = 600; or something like that, but I'm not sure how I'd implement that.
Also, I tried adding a method for checking if a day has passed, but the code ended up running like 20 times.
public int currentDay = 1;
private void HasDayPassed()
{
if (currentTime.Hour == 0)
{
Debug.Log("New day");
currentDay++;
}
}