Skip to content

Commit 6b91bd6

Browse files
authored
Script Update v8
1 parent 0ce7967 commit 6b91bd6

File tree

1 file changed

+105
-79
lines changed

1 file changed

+105
-79
lines changed

DayAndNight.cs

+105-79
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,105 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
3-
using UnityEngine;
4-
5-
public class DayAndNight : MonoBehaviour
6-
{
7-
[Range(0, 1)]
8-
public float time;
9-
public float fullDayLength;
10-
public float startTime = 0.4f;
11-
public float timeRate;
12-
public Vector3 noon;
13-
14-
[Header("Sun")]
15-
public Light sun;
16-
public Gradient sunColor;
17-
public AnimationCurve sunIntensity;
18-
19-
[Header("Moon")]
20-
public Light moon;
21-
public Gradient moonColor;
22-
public AnimationCurve moonIntensity;
23-
24-
[Header("Other Seetings")]
25-
public AnimationCurve lightingIntensityMultiplier;
26-
public AnimationCurve reflectionIntensityMultiplier;
27-
28-
public Material day;
29-
public Material night;
30-
31-
float duration = 2.0f;
32-
33-
34-
// Start is called before the first frame update
35-
void Start()
36-
{
37-
timeRate = 1 / fullDayLength;
38-
time = startTime;
39-
}
40-
41-
// Update is called once per frame
42-
void Update()
43-
{
44-
time += timeRate * Time.deltaTime;
45-
46-
if (time >= 1)
47-
time = 0;
48-
49-
sun.transform.eulerAngles = (time - 0.25f) * noon * 4.0f;
50-
moon.transform.eulerAngles = (time - 0.75f) * noon * 4.0f;
51-
52-
sun.intensity = sunIntensity.Evaluate(time);
53-
moon.intensity = moonIntensity.Evaluate(time);
54-
55-
sun.color = sunColor.Evaluate(time);
56-
moon.color = moonColor.Evaluate(time);
57-
58-
if(sun.intensity == 0 && sun.gameObject.activeInHierarchy)
59-
{
60-
sun.gameObject.SetActive(false);
61-
}
62-
else if(sun.intensity > 0 && !sun.gameObject.activeInHierarchy)
63-
{
64-
sun.gameObject.SetActive(true);
65-
}
66-
67-
if (moon.intensity == 0 && moon.gameObject.activeInHierarchy)
68-
{
69-
moon.gameObject.SetActive(false);
70-
}
71-
else if (moon.intensity > 0 && !moon.gameObject.activeInHierarchy)
72-
{
73-
moon.gameObject.SetActive(true);
74-
}
75-
76-
RenderSettings.ambientIntensity = lightingIntensityMultiplier.Evaluate(time);
77-
RenderSettings.reflectionIntensity = reflectionIntensityMultiplier.Evaluate(time);
78-
}
79-
}
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class DayAndNight : MonoBehaviour
6+
{
7+
[Range(0, 1)]
8+
public float time;
9+
public float fullDayLength;
10+
public float startTime = 0.4f;
11+
public float timeRate;
12+
public Vector3 noon;
13+
14+
[Header("Sun")]
15+
public Light sun;
16+
public Gradient sunColor;
17+
public AnimationCurve sunIntensity;
18+
19+
[Header("Moon")]
20+
public Light moon;
21+
public Gradient moonColor;
22+
public AnimationCurve moonIntensity;
23+
24+
[Header("Other Seetings")]
25+
public AnimationCurve lightingIntensityMultiplier;
26+
public AnimationCurve reflectionIntensityMultiplier;
27+
28+
[SerializeField]
29+
private GameObject clouds;
30+
[SerializeField]
31+
private GameObject lightShafts;
32+
33+
[SerializeField]
34+
private Color dayColor;
35+
[SerializeField]
36+
private Color nightColor;
37+
38+
//private float duration = 2.0f;
39+
40+
// Start is called before the first frame update
41+
void Start()
42+
{
43+
timeRate = 1 / fullDayLength;
44+
time = startTime;
45+
RenderSettings.fog = true;
46+
47+
StartCoroutine(UpdateCycle());
48+
}
49+
50+
IEnumerator UpdateCycle()
51+
{
52+
while (true)
53+
{
54+
yield return new WaitForSeconds(Time.deltaTime);
55+
56+
time += timeRate * Time.deltaTime;
57+
58+
if (time >= 1)
59+
time = 0;
60+
61+
sun.transform.eulerAngles = (time - 0.25f) * noon * 4.0f;
62+
moon.transform.eulerAngles = (time - 0.75f) * noon * 4.0f;
63+
64+
sun.intensity = sunIntensity.Evaluate(time);
65+
moon.intensity = moonIntensity.Evaluate(time);
66+
67+
sun.color = sunColor.Evaluate(time);
68+
moon.color = moonColor.Evaluate(time);
69+
70+
if (sun.intensity == 0 && sun.gameObject.activeInHierarchy)
71+
{
72+
sun.gameObject.SetActive(false);
73+
}
74+
else if (sun.intensity > 0 && !sun.gameObject.activeInHierarchy)
75+
{
76+
sun.gameObject.SetActive(true);
77+
}
78+
79+
if (moon.intensity == 0 && moon.gameObject.activeInHierarchy)
80+
{
81+
moon.gameObject.SetActive(false);
82+
}
83+
else if (moon.intensity > 0 && !moon.gameObject.activeInHierarchy)
84+
{
85+
moon.gameObject.SetActive(true);
86+
}
87+
88+
if (!moon.gameObject.activeSelf)
89+
{
90+
RenderSettings.fogColor = dayColor;
91+
clouds.SetActive(true);
92+
lightShafts.SetActive(true);
93+
}
94+
else
95+
{
96+
RenderSettings.fogColor = nightColor;
97+
clouds.SetActive(false);
98+
lightShafts.SetActive(false);
99+
}
100+
101+
RenderSettings.ambientIntensity = lightingIntensityMultiplier.Evaluate(time);
102+
RenderSettings.reflectionIntensity = reflectionIntensityMultiplier.Evaluate(time);
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)