Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityStandardAssets.ImageEffects;
- using TMPro;
- public class DayNightCycle : MonoBehaviour {
- public Light sun;
- public float secondsInFullDay = 120f;
- //[Range(0, 1)]
- public float currentTimeOfDay = 0.16f;
- [HideInInspector]
- public float timeMultiplier = 1f;
- public TextMeshProUGUI dayText;
- public int day;
- float sunInitialIntensity = 0.8f;
- public SunShafts sunShaft;
- float sunShaftIntensity;
- void Start()
- {
- day = 1;
- dayText.text = "DAY: " + day;
- sunInitialIntensity = sun.intensity;
- sunShaftIntensity = sunShaft.sunShaftIntensity;
- }
- void Update()
- {
- UpdateSun();
- currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;
- if (currentTimeOfDay >= 1)
- {
- currentTimeOfDay = 0;
- }
- if(currentTimeOfDay >= 0.40f && currentTimeOfDay <= 0.401f)
- {
- day += 1;
- dayText.text = "DAY: " + day;
- }
- }
- void UpdateSun()
- {
- sun.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 35, 170, 0);
- float sunIntensityMultiplier = 1;
- float shaftIntensityMultiplier = 1;
- if (currentTimeOfDay <= 0.23f || currentTimeOfDay >= 0.75f)
- {
- sunIntensityMultiplier = 0.2f;
- shaftIntensityMultiplier = 0f;
- }
- else if (currentTimeOfDay <= 0.25f)
- {
- sunIntensityMultiplier = Mathf.Clamp01((currentTimeOfDay - 0.25f) * (1 / 0.02f));
- shaftIntensityMultiplier = Mathf.Clamp01((currentTimeOfDay - 0.25f) * (1 / 0.02f));
- }
- else if (currentTimeOfDay >= 0.73f)
- {
- sunIntensityMultiplier = Mathf.Clamp01(1 - ((currentTimeOfDay - 0.73f) * (1 / 0.02f)));
- shaftIntensityMultiplier = Mathf.Clamp01(1 - ((currentTimeOfDay - 0.73f) * (1 / 0.02f)));
- }
- sun.intensity = sunInitialIntensity * sunIntensityMultiplier;
- sunShaft.sunShaftIntensity = sunShaftIntensity * shaftIntensityMultiplier;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement