Easy URP Day/Night Cycle in Unity with Scripting
The allure of a dynamic game world is undeniable. A day and night cycle breathes life into your virtual environments, making them feel more realistic and immersive. While there are many ways to implement this effect, this article will focus on a streamlined, script-driven approach using Unity’s Universal Render Pipeline (URP) to control the directional light and skybox exposure. We’ll ditch the complex shader graphs and animation curves for simple, effective C# scripting, proving that stunning visuals don’t require a Ph.D. in computer graphics.
Setting Up Your Scene
The most-downloaded assets on Wayline this month — sorted by what real developers actually use.
First, create a new Unity project and select the URP template. This provides the necessary rendering pipeline assets. Then, add a directional light to your scene. This light will simulate the sun, and we’ll be manipulating its color and intensity. Ensure you have a skybox material assigned to your scene’s lighting settings. You can find this under Window > Rendering > Lighting > Environment.
Scripting the Light and Skybox
Now for the fun part: scripting the day-night cycle. Create a new C# script named DayNightCycle and attach it to any GameObject in your scene (I recommend creating an empty GameObject named “DayNightCycleController”). Here’s the core code:
using UnityEngine;
public class DayNightCycle : MonoBehaviour
{
public Light directionalLight;
public Material skyboxMaterial;
public float cycleDuration = 60f; // Seconds for a full day-night cycle
[ColorUsage(true, true)] // Enable HDR color picking
public Color dayColor = Color.white;
[ColorUsage(true, true)]
public Color nightColor = Color.blue;
public float dayIntensity = 1f;
public float nightIntensity = 0.1f;
public float maxExposure = 1.3f;
public float minExposure = -0.3f;
private float _time;
void Update()
{
_time += Time.deltaTime / cycleDuration;
_time %= 1f; // Keep time between 0 and 1
// Calculate light color and intensity
directionalLight.color = Color.Lerp(nightColor, dayColor, EvaluateTime());
directionalLight.intensity = Mathf.Lerp(nightIntensity, dayIntensity, EvaluateTime());
// Adjust skybox exposure
skyboxMaterial.SetFloat("_Exposure", Mathf.Lerp(minExposure, maxExposure, EvaluateTime()));
}
//Smoother transition using a sine wave
private float EvaluateTime()
{
return 0.5f + 0.5f * Mathf.Sin(_time * 2 * Mathf.PI);
}
}
This script gradually changes the directional light’s color and intensity, as well as the skybox’s exposure. It uses Color.Lerp and Mathf.Lerp for smooth transitions between day and night values. Drag your directional light and skybox material into the corresponding fields in the Inspector.
Exposing the Skybox
One common pitfall is forgetting to expose the _Exposure property on your skybox shader! Make sure your skybox material uses a shader that exposes this property (most URP skybox shaders do). If not, you’ll need to create a custom shader or modify an existing one. This is crucial for controlling the sky’s brightness during the day/night cycle.
Customizing the Cycle
The beauty of this approach is its flexibility. Adjust the cycleDuration, dayColor, nightColor, dayIntensity, nightIntensity, maxExposure, and minExposure variables in the Inspector to fine-tune the cycle to your liking. Experiment with different color palettes and intensity levels to create a unique atmosphere. For example, a longer cycle duration makes the transitions feel slower and more gradual.
Common Mistakes and Solutions
A common mistake is forgetting the [ColorUsage(true, true)] attribute above the dayColor and nightColor variables. Without this, you won’t be able to select HDR colors, limiting the vibrancy of your light. Another issue is using a linear time progression. The EvaluateTime() function introduces a sine wave, making the sunrise and sunset appear more natural by slowing down the color and intensity changes at those critical moments.
Beyond the Basics
This is just the foundation. You can expand upon this system by adding:
- Stars: Activate and fade in a starfield GameObject during the night.
- Moon: Introduce a second, weaker directional light to simulate the moon.
- Fog: Adjust the scene’s fog density based on the time of day.
Remember, the key is to start simple and gradually add complexity. This script-driven approach offers a balance of control and ease of use, allowing you to create stunning day/night cycles in your URP Unity projects without getting bogged down in complex shader programming.