Learning how to make a day night cycle script is honestly one of those "level up" moments for any aspiring game developer. There's just something about watching the sun dip below the horizon and seeing the stars pop out in your own game world that makes everything feel real. It adds a layer of immersion that static lighting can't touch. If you've been staring at a bright, noon-day sun in your project and wondering how to get things moving, you're in the right place.
The good news is that you don't need a degree in astrophysics to get this working. At its core, a day-night cycle is just a bit of math tied to a timer. Whether you're working in Unity, Godot, or even Roblox, the logic remains pretty much the same. You're basically taking a light source and rotating it around your world.
The basic logic behind the sun
Before we jump into the lines of code, let's talk about what we're actually trying to achieve. In most game engines, the sun is represented by a "Directional Light." Unlike a lamp or a torch, a directional light doesn't have a specific position; it just has an angle. It mimics how the sun works in real life—the rays are parallel, and it hits everything in the scene from a specific direction.
To figure out how to make a day night cycle script, you have to think of that light as being on a giant pivot. If you rotate that light 360 degrees, you've completed a full day. Usually, we want the sun to be at its peak at 90 degrees (noon) and hidden at 270 degrees (midnight).
The trick is controlling how fast that rotation happens. If you let it rotate based on the actual time of day, your players will be waiting 12 hours for a sunset. That's probably not what you want unless you're making a hyper-realistic simulator. Most games opt for a 10, 20, or 30-minute loop.
Setting up your variables
Every script needs a foundation. When you start writing your script, you'll want to define a few key variables. This makes it easier to tweak the "vibe" of your game later without rewriting everything.
First, you need a Day Duration. This is how many real-world seconds or minutes a full in-game day takes. I usually like to set this to something like 600 seconds (10 minutes) to start with. It's fast enough to test but slow enough to feel natural.
Next, you'll want a Current Time variable. This is a float (a decimal number) that keeps track of where we are in the cycle. You can track this from 0 to 1, where 0 is the start of the day and 1 is the very end. This makes the math way easier because you can just multiply that 0-to-1 value by 360 to get your rotation.
Don't forget to reference your Sun Light. Your script needs to know which light it's supposed to be spinning. If you don't link the light to the script, you'll just be rotating invisible numbers in the background.
Writing the movement code
Now for the meat of the project. To make the sun actually move, you're going to use your engine's update loop—the part of the code that runs every single frame.
You'll want to increase your Current Time variable by a small amount every frame. Since the frame rate can vary (someone might be playing at 30 FPS while someone else is at 144 FPS), you have to multiply your time increase by "Delta Time." This ensures the sun moves at the same speed regardless of how beefy the player's computer is.
Once you have your time moving, you apply that to the rotation of the directional light. In C#, it might look something like this: sun.transform.localRotation = Quaternion.Euler((currentTime * 360f) - 90f, 0, 0);. The "- 90f" part is just a little offset to make sure the sun starts at the horizon rather than directly overhead at the start of your timer.
Making it look good with colors
If you just rotate a white light, your game is going to look a bit weird. The sun will go down, but the sky might stay bright blue, or the shadows might look too harsh for a midnight setting. To really nail how to make a day night cycle script, you need to handle the colors.
Think about a real sunset. The light turns orange, then deep red, then a dark purple or blue as night falls. Most modern game engines have something called a "Gradient" tool. You can set up a gradient that goes from bright yellow (noon) to orange (sunset) to dark blue (midnight).
Inside your script, you can tell the light to change its color based on that Current Time variable we talked about earlier. As the value moves from 0.5 (evening) to 0.7 (dusk), the script "samples" the gradient and updates the light's color. It's a small touch, but it makes a massive difference in how the game feels.
Managing the skybox
The skybox is the "wrapper" around your world. If you move the sun but the skybox stays midday blue, the illusion is broken. Some engines have "Procedural Skyboxes" that automatically update based on the sun's position. If you're lucky enough to use one of those, your job is easy—just point the skybox to your sun object.
If you're doing it manually, you might need to adjust the skybox exposure or tint alongside the light. You want the world to actually get dark. At midnight, you should probably turn the intensity of your sun light down to almost zero, or even switch it out for a dim "Moon" light that casts a faint blue glow.
Handling the "Night" problem
One thing people often forget when learning how to make a day night cycle script is that night isn't just "the sun is gone." It's a different state of the game. When the sun goes down, you might want streetlights to turn on, or maybe certain enemies should start spawning.
You can handle this with a simple "if" statement. Check if your Current Time is between, say, 0.7 and 0.2 (the night hours). If it is, trigger an event.
It's much more efficient to have a "toggle" rather than checking every single frame. For example, you can have a boolean variable called isNight. When the sun passes the horizon, flip isNight to true and run a function that turns on all the lights in your town. When the sun comes back up, flip it back to false and turn them off. This saves a lot of processing power and keeps your code clean.
Optimization and polish
Once you have the basic loop running, you might notice the shadows flickering or the sun "stuttering" if the movement is too slow. This usually happens because of floating-point precision or just how the engine handles rotations.
One way to fix this is to not update the rotation every single frame if the day is very long. You could update it every 0.1 seconds instead. Most players won't notice the sun jumping by a tiny fraction of a degree, and it can save a little bit of CPU overhead.
Also, consider the moon. A lot of developers just use one light for the sun and let it go under the floor. But if you want a beautiful night, you should add a second directional light facing the opposite way. When the sun goes down, the moon comes up. You can sync them so they're always 180 degrees apart. Just make sure the moon light is much dimmer and maybe a bit more "cool" in color.
Wrapping it up
Learning how to make a day night cycle script is a great introduction to handling time and environmental systems in game dev. It's a project that combines basic math, light settings, and a bit of logic to create something visible and impactful.
Don't be afraid to experiment with the timing. Some games feel better with long, lingering sunsets, while others need a quick transition so players aren't stuck in the dark for too long. Once you have the basic script running, you can keep adding layers—stars that fade in, weather systems, or even different lengths for seasons.
The most important part is just getting that first rotation working. Once the sun moves, the rest of the atmosphere usually falls into place with a little bit of tweaking. Happy coding, and enjoy watching those first few sunsets in your game world!