diliupg

TimeController

Oct 21st, 2021 (edited)
140
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using TMPro;
  6.  
  7. // from Ketra Games video on Youtube (https://www.youtube.com/watch?v=L4t2c1_Szdk)
  8. public class TimeController : MonoBehaviour
  9. {
  10.     [SerializeField]
  11.     private float timeMultiplier;
  12.  
  13.     [SerializeField]
  14.     private float startHour;
  15.  
  16.     [SerializeField]
  17.     private TextMeshProUGUI timeText;
  18.  
  19.     [SerializeField]
  20.     private Light sunLight;
  21.  
  22.     [SerializeField]
  23.     private float sunriseHour;
  24.  
  25.     [SerializeField]
  26.     private float sunsetHour;
  27.  
  28.     [SerializeField]
  29.     private Color dayAmbientLight;
  30.  
  31.     [SerializeField]
  32.     private Color nightAmbientLight;
  33.  
  34.     [SerializeField]
  35.     private AnimationCurve lightChangeCurve;
  36.  
  37.     [SerializeField]
  38.     private float maxSunLightIntensity;
  39.  
  40.     [SerializeField]
  41.     private Light moonLight;
  42.  
  43.     [SerializeField]
  44.     private float maxMoonLightIntensity;
  45.  
  46.     private DateTime currentTime;
  47.  
  48.     private TimeSpan sunriseTime;
  49.  
  50.     private TimeSpan sunsetTime;
  51.  
  52.     // Start is called before the first frame update
  53.     void Start()
  54.     {
  55.         currentTime = DateTime.Now.Date + TimeSpan.FromHours(startHour);
  56.  
  57.         sunriseTime = TimeSpan.FromHours(sunriseHour);
  58.         sunsetTime = TimeSpan.FromHours(sunsetHour);
  59.     }
  60.  
  61.     // Update is called once per frame
  62.     void Update()
  63.     {
  64.         UpdateTimeOfDay();
  65.         RotateSun();
  66.         UpdateLightSettings();
  67.     }
  68.  
  69.     private void UpdateTimeOfDay()
  70.     {
  71.         currentTime = currentTime.AddSeconds(Time.deltaTime * timeMultiplier);
  72.  
  73.         if (timeText != null)
  74.         {
  75.             timeText.text = currentTime.ToString("HH:mm");
  76.         }
  77.     }
  78.  
  79.     private void RotateSun()
  80.     {
  81.         float sunLightRotation;
  82.  
  83.         if (currentTime.TimeOfDay > sunriseTime && currentTime.TimeOfDay < sunsetTime)
  84.         {
  85.             TimeSpan sunriseToSunsetDuration = CalculateTimeDifference(sunriseTime, sunsetTime);
  86.             TimeSpan timeSinceSunrise = CalculateTimeDifference(sunriseTime, currentTime.TimeOfDay);
  87.  
  88.             double percentage = timeSinceSunrise.TotalMinutes / sunriseToSunsetDuration.TotalMinutes;
  89.  
  90.             sunLightRotation = Mathf.Lerp(0, 180, (float)percentage);
  91.         }
  92.         else
  93.         {
  94.             TimeSpan sunsetToSunriseDuration = CalculateTimeDifference(sunsetTime, sunriseTime);
  95.             TimeSpan timeSinceSunset = CalculateTimeDifference(sunsetTime, currentTime.TimeOfDay);
  96.  
  97.             double percentage = timeSinceSunset.TotalMinutes / sunsetToSunriseDuration.TotalMinutes;
  98.  
  99.             sunLightRotation = Mathf.Lerp(180, 360, (float)percentage);
  100.         }
  101.  
  102.         sunLight.transform.rotation = Quaternion.AngleAxis(sunLightRotation, Vector3.right);
  103.     }
  104.  
  105.     private void UpdateLightSettings()
  106.     {
  107.         float dotProduct = Vector3.Dot(sunLight.transform.forward, Vector3.down);
  108.         sunLight.intensity = Mathf.Lerp(0, maxSunLightIntensity, lightChangeCurve.Evaluate(dotProduct));
  109.         moonLight.intensity = Mathf.Lerp(maxMoonLightIntensity, 0, lightChangeCurve.Evaluate(dotProduct));
  110.         RenderSettings.ambientLight = Color.Lerp(nightAmbientLight, dayAmbientLight, lightChangeCurve.Evaluate(dotProduct));
  111.     }
  112.  
  113.     private TimeSpan CalculateTimeDifference(TimeSpan fromTime, TimeSpan toTime)
  114.     {
  115.         TimeSpan difference = toTime - fromTime;
  116.  
  117.         if (difference.TotalSeconds < 0)
  118.         {
  119.             difference += TimeSpan.FromHours(24);
  120.         }
  121.  
  122.         return difference;
  123.     }
  124. }
RAW Paste Data