How to Add a Day-Night Cycle to Your Game
A day-night cycle is a clock plus a set of color curves plus the rules that change when it gets dark. Here is how to build all three.

Key takeaways
- Drive everything from one game clock (a number from 0 to 24) that you can pause, speed up and save, never from the real system time by accident.
- Interpolate sky, sun and ambient colors between four to six keyframes, and keep nights dark in mood but bright enough to play.
- The cycle earns its place through gameplay: spawns, shops, crops and vision that change with the time, announced before they happen.
To add a day-night cycle, give your game a single clock that runs from 0 to 24 hours over a set number of real minutes, then let that clock drive three things: the light (sky color, sun angle and brightness, ambient tint), events (what spawns, what opens, what grows) and feedback (a clock on the HUD, a sound cue at dusk). Pick 4 to 6 color keyframes across the day and blend between them. Everything else in the game asks the clock what time it is instead of keeping its own timer.
One clock that everything reads
The mistake most first cycles make is to animate the sun directly, with a tween that loops forever. That looks fine until something else needs to know the time: a shop that closes at night, a monster that spawns after dark, a save file. Start with a number instead.
- timeOfDay: a float from 0 to 24. Every frame, add `delta × 24 ÷ dayLengthSeconds`, and wrap past 24 while adding 1 to a day counter.
- timeScale: a multiplier for sleeping (skip to morning), fast-forward or debug. Set it to 0 to pause time in menus and dialogue.
- Phases: dawn, day, dusk, night, derived from the hour (for example dawn 5 to 7, day 7 to 18, dusk 18 to 20, night 20 to 5). Fire an event when the phase changes, so other systems listen instead of polling.
Day length depends on the game. Survival games commonly use 10 to 20 real minutes per full day; the survival game guide explains why around two-thirds day and one-third night works for pressure. Cozy and farming games stretch to 12 to 20 minutes and often stop time indoors, as the farming guide discusses. A purely visual cycle in an action game can be as short as 4 to 6 minutes, so players actually see it turn during a session.
The light: keyframes, not math
Do not compute sky color from a formula. Pick colors by eye at a few moments and blend between them. Starting keyframes for a 3D scene:
- 06:00 dawn: sky horizon soft pink-orange, sun low and warm, sun intensity about 30% of noon, long shadows.
- 12:00 noon: pale blue sky, near-white sun high overhead, full intensity, short shadows.
- 18:30 dusk: deep orange horizon, sun intensity dropping toward 20%, ambient light shifting to purple.
- 21:00 to 04:00 night: dark blue sky (not black), a cool moonlight at 15 to 25% of noon's brightness, ambient light lifted just enough to read shapes.
Blend in a perceptual color space or at least ease the blend, so dusk does not pass through gray. Rotate the sun on one axis across the day, and swap to a moon light (same direction logic, cooler color) at night rather than letting shadows from a sun below the horizon point upward. In a 2D game, the same idea becomes a full-screen tint: a multiply color over the scene (warm at dawn and dusk, dark blue at night) plus additive glows for lamps and windows.
Night must stay playable
Night should feel different, not be a black screen. Keep the player, enemies and interactive objects readable at night: a subtle rim light on characters, lamps and fires that cast real light, and glowing eyes or lanterns on enemies. If testers turn their phone brightness up to play your night, it is too dark. A horror game can push darkness further, but even there the path stays readable.
On Startbase: the level look and the running cycle
In the World Editor, each level has a look with a time of day from dawn to night. In a 3D game the sky, the sun and the ambient light follow that time until you set one yourself, and fog and glow sit alongside; in a 2D game the time of day tints the level. That look is one fixed moment saved with the level, which makes it the right place to pick and preview your keyframe colors. The cycle that moves through those moments while the game runs is game logic: ask the agent to drive the same sky and sun settings from the game clock.
Make the time of day change play
A cycle that only changes colors is a screensaver. Give each phase at least one rule players can plan around:
- Spawns: different enemies at night, or more of them (1.5 to 2 times the rate is a common start). Despawn or retreat them at dawn so mornings feel like relief.
- Vision: shrink the player's sight radius by 30 to 50 percent at night if your game has fog of war; torches give it back in a small circle.
- Schedules: shops open 8 to 18, a bridge keeper who only lets you cross at noon, villagers walking home at dusk.
- Growth and timers: crops and cooldowns that advance per in-game day, keyed to the day counter.
- Rest: a bed or campfire that skips to morning by setting timeScale high or jumping the clock, with a short fade.
Announce changes before they bite. A sound cue and a HUD color shift one in-game hour before night (about 30 to 60 real seconds) lets players head home. Surprise darkness feels unfair; expected darkness feels like tension.
Bugs a day-night cycle usually causes
- 01Midnight wraparound. A rule like "night is from 20 to 5" fails a simple `hour > 20 && hour < 5` check. Handle ranges that cross 24 explicitly.
- 02Skipped events. A big time jump (sleeping, a lag spike, the tab in the background) moves the clock past 18:00 without ever firing dusk. Check every phase boundary between the old and new time, not just the current phase.
- 03Background tabs. Browsers throttle hidden tabs, then hand back a huge frame delta. Clamp delta to about 0.1 seconds per frame, or decide deliberately that time passes while away.
- 04Save and load. Saving the day counter but not timeOfDay (or the reverse) puts players back at dawn on day 1. Save both, plus timeScale if rest can be interrupted.
- 05Lighting pops. Lights that switch on exactly at 20:00 all at once look mechanical. Stagger lamps over a few seconds, and fade, never toggle.
- 06Baked shadows and lightmaps. Static, precomputed lighting does not move with the sun. Keep lighting real-time for anything the sun should affect, within your phone budget.
- 07Multiplayer drift. Each player running their own clock disagrees within minutes. The server owns the clock and clients interpolate.
A day-night cycle brief to paste
Add a day-night cycle to my 3D island game. One game clock from 0 to 24 with a day counter; a full day takes 12 real minutes (day 7:00 to 19:00, dusk 19 to 21, night 21 to 5, dawn 5 to 7). Time pauses in menus and dialogue. Blend sky, sun color, sun intensity and ambient light between keyframes at 06:00, 12:00, 18:30 and 22:00 using the level's current look as the noon colors; switch to a cool moonlight at night and keep the player and enemies readable. Fire phase-change events, checking every boundary crossed on big time jumps. At night, enemies spawn at twice the rate and the sight radius shrinks by 40%; lamps fade on over 3 seconds, staggered. One in-game hour before night, play a bell and tint the HUD clock orange. Sleeping in a bed skips to 06:00 with a fade. Save timeOfDay and the day counter. Clamp frame delta to 0.1 s. Expose day length and night darkness as dials.
Play two full cycles before judging it: once watching the sky, once actually playing. Save a checkpoint first, so you can compare different day lengths without losing the version that worked. When it feels right, publish and share it to the Arcade.
Frequently asked questions
How long should a day-night cycle be in a game?
Common ranges are 10 to 20 real minutes per full day for survival and cozy games, and 4 to 6 minutes for purely visual cycles in short action sessions. Make it a dial and tune it by playing.
How do I make night dark without making the game unplayable?
Use a dark blue sky instead of black, a cool moonlight at roughly 15 to 25 percent of daytime brightness, real light from lamps and fires, and rim light or glows on characters and enemies so they stay readable.
Should my game use real-world time for day and night?
Only if you want players to return at specific real times, as in virtual pets. Most games use their own clock so they can pause it, skip it and save it.
Why does my day-night event not fire after sleeping?
The clock jumped past the boundary in one step. Check every phase boundary between the previous and the new time, and handle ranges that cross midnight explicitly.

