Implement a Day/Night cycle

In this demo, we want to implement a basic day/night cycle. To do that, we will attach this script to the Direct Light object:

using UnityEngine;public class DayNightCycle : MonoBehaviour {   public event System.EventHandler OnChanged;   public float dayDuration = 10.0f;   public bool isNight { get; private set; }   private Color dayColor;   private Light lightComponent;   void Start()   {       lightComponent = GetComponent<Light>();       dayColor = lightComponent.color;   }   void Update()   {  Color nightColor = Color.white * 0.1f;       float lightIntensity = 0.5f +                     Mathf.Sin(Time.time * 2.0f * Mathf.PI / dayDuration) / 2.0f;       if (isNight != (lightIntensity < 0.3))       {           isNight = !isNight;           if (OnChanged != null) OnChanged(this, System.EventArgs.Empty); ...

Get Unity Artificial Intelligence Programming - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.