Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- public class DayNightSystem : MonoBehaviour
- {
- public Light directionalLight;
- public float dayDurationInSeconds = 24.0f; // Adjust the duration of a full day in seconds
- public int currentHour; private int seconds = 0;
- float currentTimeOfDay = 0.35f; float currentTimeOfDayForSeconds = 0f; // 8:00 AM
- public List<SkyboxTimeMapping> timeMappings;
- float blendedValue = 0.0f;
- bool lockNextDayTrigger = false;
- public TextMeshProUGUI timeUI;
- public WeatherSystem weatherSystem;
- // Update is called once per frame
- void Update()
- {
- // Calculate the current time of day based on the game time
- currentTimeOfDay += Time.deltaTime / dayDurationInSeconds; currentTimeOfDayForSeconds += Time.deltaTime / (dayDurationInSeconds / 40);
- currentTimeOfDay %= 1; currentTimeOfDayForSeconds %= 1; // Ensure it stays between 0 and 1;
- currentHour = Mathf.FloorToInt(currentTimeOfDay * 24); seconds = Mathf.FloorToInt(currentTimeOfDayForSeconds * 59);
- if (seconds < 10) { if (currentHour > 12) { timeUI.text = $"{currentHour-11}:0{seconds}"; } else { timeUI.text = $"{currentHour}:0{seconds}"; } } else { if (currentHour > 12) { timeUI.text = $"{currentHour - 11}:{seconds}"; } else { timeUI.text = $"{currentHour}:{seconds}"; } }
- //timeUI.text = $"{currentHour}:00"; Replaced with below code ^
- // Update the directional light's rotation
- directionalLight.transform.rotation = Quaternion.Euler(new Vector3((currentTimeOfDay * 360) - 90, 170, 0));
- // Update the skybox material based on the time of day
- if (weatherSystem.isSpecialWeather == false)
- {
- UpdateSkybox();
- }
- if (currentHour == 0 && lockNextDayTrigger == false)
- {
- TimeManager.Instance.TriggerNextDay();
- lockNextDayTrigger = true;
- }
- if (currentHour != 0)
- {
- lockNextDayTrigger = false;
- }
- }
- private void UpdateSkybox()
- {
- // Find the appropriate skybox material for the current hour
- Material currentSkybox = null;
- foreach (SkyboxTimeMapping mapping in timeMappings)
- {
- if (currentHour == mapping.hour)
- {
- currentSkybox = mapping.skyboxMaterial;
- if (currentSkybox.shader != null)
- {
- if (currentSkybox.shader.name == "Custom/SkyboxTransition")
- {
- blendedValue += Time.deltaTime;
- blendedValue = Mathf.Clamp01(blendedValue);
- currentSkybox.SetFloat("_TransitionFactor", blendedValue);
- }
- else
- {
- blendedValue = 0;
- }
- }
- break;
- }
- }
- if (currentSkybox != null)
- {
- RenderSettings.skybox = currentSkybox;
- }
- }
- }
- [System.Serializable]
- public class SkyboxTimeMapping
- {
- public string phaseName;
- public int hour; // The hour of the day (0-23)
- public Material skyboxMaterial; // The corresponding skybox material for this hour;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement