Hello people, I am making a video game where I have made a cycle of day and night, I also made post processing effects with different volumes for day and night, now I am trying to make it change from one volume to another when it is day or night, but this with a dissolve transition or something like that, so that the change from one volume to another is not so abrupt.
Here I leave the code of the day and night cycle, thanks and regards
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class DayNight : MonoBehaviour
{
[SerializeField]
private float timeMultiplier;
[SerializeField]
private float startHour;
[SerializeField]
private TextMeshProUGUI timeText;
[SerializeField]
private Light sunLight;
[SerializeField]
private float sunriseHour;
[SerializeField]
private float sunsetHour;
[SerializeField]
private Color dayAmbientLight;
[SerializeField]
private Color nightAmbientLight;
[SerializeField]
private AnimationCurve lightChangeCurve;
[SerializeField]
private float maxSunLightIntensity;
[SerializeField]
private Light moonLight;
[SerializeField]
private float maxMoonLightIntensity;
private DateTime currentTime;
private TimeSpan sunriseTime;
private TimeSpan sunsetTime;
// Start is called before the first frame update
void Start()
{
currentTime = DateTime.Now.Date + TimeSpan.FromHours(startHour);
sunriseTime = TimeSpan.FromHours(sunriseHour);
sunsetTime = TimeSpan.FromHours(sunsetHour);
}
// Update is called once per frame
void Update()
{
UpdateTimeOfDay();
RotateSun();
UpdateLightSettings();
}
private void UpdateTimeOfDay()
{
currentTime = currentTime.AddSeconds(Time.deltaTime * timeMultiplier);
if (timeText != null)
{
timeText.text = currentTime.ToString("HH:mm");
}
}
private void RotateSun()
{
float sunLightRotation;
if (currentTime.TimeOfDay > sunriseTime && currentTime.TimeOfDay < sunsetTime)
{
TimeSpan sunriseToSunsetDuration = CalculateTimeDifference(sunriseTime, sunsetTime);
TimeSpan timeSinceSunrise = CalculateTimeDifference(sunriseTime, currentTime.TimeOfDay);
double percentage = timeSinceSunrise.TotalMinutes / sunriseToSunsetDuration.TotalMinutes;
sunLightRotation = Mathf.Lerp(0, 180, (float)percentage);
}
else
{
TimeSpan sunsetToSunriseDuration = CalculateTimeDifference(sunsetTime, sunriseTime);
TimeSpan timeSinceSunset = CalculateTimeDifference(sunsetTime, currentTime.TimeOfDay);
double percentage = timeSinceSunset.TotalMinutes / sunsetToSunriseDuration.TotalMinutes;
sunLightRotation = Mathf.Lerp(180, 360, (float)percentage);
}
sunLight.transform.rotation = Quaternion.AngleAxis(sunLightRotation, Vector3.right);
}
private void UpdateLightSettings()
{
float dotProduct = Vector3.Dot(sunLight.transform.forward, Vector3.down);
sunLight.intensity = Mathf.Lerp(0, maxSunLightIntensity, lightChangeCurve.Evaluate(dotProduct));
moonLight.intensity = Mathf.Lerp(maxMoonLightIntensity, 0, lightChangeCurve.Evaluate(dotProduct));
RenderSettings.ambientLight = Color.Lerp(nightAmbientLight, dayAmbientLight, lightChangeCurve.Evaluate(dotProduct));
}
private TimeSpan CalculateTimeDifference(TimeSpan fromTime, TimeSpan toTime)
{
TimeSpan difference = toTime - fromTime;
if (difference.TotalSeconds < 0)
{
difference += TimeSpan.FromHours(24);
}
return difference;
}
}
↧