Godot Day Night Cycle: How to Implement
In this article
To implement a day/night cycle in Godot, you animate a DirectionalLight3D's rotation and adjust its color and intensity over time, typically using a script that tracks a time-of-day variable. The core approach is simple: connect a timer or use _process() to increment a time value, then use that value to set the light's rotation and color.
Understanding the Core Concept
A day/night cycle simulates the sun's movement across the sky. In Godot, you achieve this by rotating a directional light (which acts as the sun) around the X or Z axis. As the light rotates, its angle changes, creating the illusion of sunrise, noon, sunset, and night. You also need to adjust the light's color and intensity to mimic the warm hues of dawn/dusk and the dim, cool tones of night.
Setting Up the Scene
Before writing any code, set up your scene properly:
- Create a
DirectionalLight3Dnode and name itSun. EnableShadowfor realistic lighting. - Optionally, add a
WorldEnvironmentnode to control ambient light and sky. Set itsBackgroundtoSkyand assign aProceduralSkyMaterialfor a dynamic sky. - Add a script to the root node of your scene (or the light itself) to manage the cycle.
Basic Script Implementation
Here's a minimal script that rotates the sun and changes its color based on a time variable (0 to 24 hours). Attach this to the root node:
extends Node3D
@export var sun_path: NodePath
@onready var sun: DirectionalLight3D = get_node(sun_path)
var time_of_day: float = 12.0 # Start at noon
var day_length_seconds: float = 120.0 # Full day in real seconds
func _process(delta):
time_of_day += delta * (24.0 / day_length_seconds)
if time_of_day >= 24.0:
time_of_day = 0.0
update_sun()
func update_sun():
var angle = (time_of_day / 24.0) * 360.0
sun.rotation_degrees.x = angle - 90.0 # Start at sunrise
# Color and intensity based on time
if time_of_day > 6.0 and time_of_day < 18.0:
# Daytime
sun.light_color = Color(1, 1, 1)
sun.light_energy = 1.0
else:
# Night
sun.light_color = Color(0.2, 0.3, 0.5)
sun.light_energy = 0.1
This is a basic version. For smoother transitions, use lerp() to interpolate between colors and energies.
Using Curves for Smooth Transitions
Hard switching between day and night looks jarring. Use Curve resources to define how color and intensity change over time. Create a Curve for the sun's energy and a Gradient for its color. Then sample these based on time_of_day.
@export var energy_curve: Curve
@export var color_gradient: Gradient
func update_sun():
var t = time_of_day / 24.0
sun.light_energy = energy_curve.sample(t) * max_intensity
sun.light_color = color_gradient.sample(t)
You can create these curves in the inspector, plotting values for each hour. This gives you precise control over the feel of your cycle.
Adjusting the Sky and Ambient Light
For a convincing cycle, the sky and ambient light must change too. If you're using a ProceduralSkyMaterial, you can adjust its sky_top_color, sky_horizon_color, and ground_bottom_color based on time. Similarly, set the WorldEnvironment's ambient light color and energy to match the time of day.
Common Problems and Solutions
- Shadows disappear at night: The directional light's shadow may not render if the light is below the horizon. Set
shadow_enabledto true and adjust theshadow_biasto avoid artifacts. - Light rotates but doesn't look like the sun: Make sure the light's rotation is around the correct axis. Typically, rotating around X works for a north-south sun path.
- Cycle is too fast/slow: Adjust
day_length_secondsto your desired real-time duration. For a 10-minute day, set it to 600. - Colors look washed out: Use a
WorldEnvironmentwith proper exposure and contrast settings. You may also need to adjust the light'slight_energyandlight_colorfor each phase.
Alternative Approaches
If you prefer a node-based approach, you can use an AnimationPlayer to animate the sun's rotation and properties. Create an animation track for the light's rotation and keyframes for color/energy. This is less flexible for dynamic time control but works well for fixed-length cycles.
For 2D games, use a CanvasModulate node to tint the entire scene, and change its color based on time. You can also rotate a TextureRect with a sun sprite.
Performance Considerations
The day/night cycle itself is cheap; the main cost comes from dynamic shadows and sky updates. To optimize:
- Only update the sky and light properties when the time value changes significantly (e.g., every 0.1 seconds).
- Use lower resolution shadow maps if performance is an issue.
- Avoid changing the
ProceduralSkyMaterialevery frame; update it at a lower frequency.
Frequently Asked Questions
How do I make the cycle start at a specific time?
Set the time_of_day variable to the desired hour (0-24). For example, to start at sunrise (6 AM), set it to 6.0.
Can I pause the day/night cycle?
Yes, simply stop updating time_of_day. You can use a boolean flag like cycle_enabled and check it in _process() before updating.
How do I sync the cycle with a real-world clock?
Use Time.get_unix_time_from_system() to get the current real-world time and map it to your cycle. For example, time_of_day = (Time.get_unix_time_from_system() % 86400) / 3600.0 gives you the current hour.
My light doesn't cast shadows at night. Why?
If the directional light's rotation is below the horizon (angle > 180 or < 0), shadows won't cast. You can disable shadows at night to save performance, or keep the light slightly above the horizon with very low intensity.
Can I have multiple lights for different times?
Yes, you can use more than one directional light, but it's simpler to adjust the properties of a single light. Multiple lights can be used for special effects like moonlight, but they add overhead.
How do I make the sky change color smoothly?
Use a Gradient for the sky colors and sample it with the same time variable. Assign the sampled colors to the ProceduralSkyMaterial's properties each frame or at intervals.