CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Follow publication

Creating a basic day-and-night cycle in Unity/C#

Let’s discover how to create a nice ambiance for our scene!

Mina Pêcheux
CodeX
Published in
14 min readDec 14, 2022

--

The majority of games rely on the notion of time to some extent; some include it in their gameplay, other make it a central narrative focus… and other use it to give some atmosphere to the scene!

So, in this tutorial, we are going to discover how to create a simple day-and-night mechanic for a 2D Unity/C# game to make our own sun and moon rise and shine…

This tutorial is available in video or text format — see below for the text version :)

Check out the Github repo with all my Unity tutorials! 🚀

As a quick mouth-watering teaser, here is a demo of the final result:

You see that, in the end, we will have this overall change in the global lighting of the scene as time goes by, and we will also have little spots on buildings and units to better highlight them at night. Finally, we will keep track of the current hour of the day with this clock in the top-right corner of the UI.

Note: In this episode, I’ll start from the Unity project where I implemented my RTS collector AI, so that I already have some nice assets to try my system on! So if you’re curious to see how I prepared this level, and how I gave my little trucks this simple gather-and-deliver logic for wood and minerals, you can either check out the article I made on the topic… or just go ahead and get the assets directly from the Github! 🚀

Alright, and with all that said, let’s get to it and discover how our day-and-night cycle will work :)

⏱ Creating time marks

To begin with, we’re going to discuss a bit how this system will be setup.

--

--

CodeX
CodeX

Published in CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Mina Pêcheux
Mina Pêcheux

Written by Mina Pêcheux

I’m a freelance full-stack web & game developer. I’m passionate about topics like CGI, music, data science and more! Find me at: https://minapecheux.com :)

No responses yet

Write a response