

A day and night cycle can dramatically improve the atmosphere and immersion of your Unity game. It involves smoothly transitioning lighting, sky colors, and sometimes environmental effects over time to simulate real-world day/night behavior.
Below is a step-by-step guide for creating a flexible and efficient day-night system in Unity.
Start with a basic 3D scene containing:
Rename your Directional Light to Sun to keep things organized.
Set the Directional Light as follows:
Later, we’ll rotate this light to simulate the sun moving across the sky.
Create a new C# script named DayNightCycle.cs and attach it to an empty GameObject called DayNightController.
This script will rotate the sun, adjust intensity, and optionally change ambient lighting.
using UnityEngine;
public class DayNightCycle : MonoBehaviour
{
[Header("Sun Settings")]
public Light sun; // Assign your directional light
public float dayDuration = 120f; // Total seconds for a full day
public float maxIntensity = 1f;
public float minIntensity = 0f;
[Header("Sky Settings")]
public Material skyboxMaterial;
public Color daySkyColor = Color.cyan;
public Color nightSkyColor = Color.black;
private float time = 0f;
void Update()
{
// Increment time
time += Time.deltaTime;
if (time > dayDuration) time = 0f;
// Calculate normalized time (0-1)
float t = time / dayDuration;
// Rotate sun
sun.transform.localRotation = Quaternion.Euler((t * 360f) - 90f, 170f, 0f);
// Adjust sun intensity
if (t < 0.25f) // Morning
sun.intensity = Mathf.Lerp(minIntensity, maxIntensity, t / 0.25f);
else if (t < 0.75f) // Day
sun.intensity = maxIntensity;
else // Evening
sun.intensity = Mathf.Lerp(maxIntensity, minIntensity, (t - 0.75f) / 0.25f);
// Adjust skybox color
if (skyboxMaterial != null)
skyboxMaterial.SetColor("_Tint", Color.Lerp(nightSkyColor, daySkyColor, sun.intensity / maxIntensity));
}
}
If you want smoother transitions:
For more realism, adjust ambient light via RenderSettings:
RenderSettings.ambientLight = Color.Lerp(Color.black, Color.white, sun.intensity / maxIntensity);
This will make the scene darker at night and brighter during the day, affecting all unlit objects.
You can add a second Directional Light for the Moon:
Stars can be a particle system or a cubemap skybox with emission adjusted at night.
Rain, fog, or clouds can be synced to the day-night cycle. Example:
RenderSettings.fogColor = Color.Lerp(nightSkyColor, daySkyColor, sun.intensity / maxIntensity);
Run the scene and observe:
A day-night cycle in Unity is a combination of light rotation, intensity control, and environmental adjustments. With a simple script, you can create an immersive world that reacts realistically over time. Once implemented, you can expand with weather, moon, stars, and gameplay effects for full dynamic realism.