Unity 2D Day/Night


I hate to ask this question due to the stupidity of it, but there seems to be nowhere online where someone shows how to make a day night cycle in UNITY 2D, not 3D. So I thought we could perhaps figure out a solution on this page and have a place where people can find the solution to the problem.

PS. I really want to know myself how to make a day/night cycle in unity 2D :slight_smile:


I got inspired and wrote this just now. It works if you follow the directions in the script. Have fun with it!!

using UnityEngine;
using System.Collections;

public class TestScript : MonoBehaviour {

	private int dayLength;   //in minutes
	private int dayStart;
	private int nightStart;   //also in minutes
	private int currentTime;
	public float cycleSpeed;
	private bool isDay;
	private Vector3 sunPosition;
	public Light sun;
	public GameObject earth;

	// Day and Night Script for 2d,
	// Unity needs one empty GameObject (earth) and one Light (sun)
	// make the sun a child of the earth
	// reset the earth position to 0,0,0 and move the sun to -200,0,0
	// attach script to sun
	// add sun and earth to script publics
	// set sun to directional light and y angle to 90

	void Start() {
		dayLength = 1440;
		dayStart = 300;
		nightStart = 1200;
		currentTime = 720;
		StartCoroutine( TimeOfDay ());
		earth = gameObject.transform.parent.gameObject;

	void Update() {

		if (currentTime > 0 && currentTime < dayStart) {
			isDay =false;
			sun.intensity = 0;
		} else if (currentTime >= dayStart && currentTime < nightStart) {
			isDay = true;
			sun.intensity = 1;
		} else if (currentTime >= nightStart && currentTime < dayLength) {
			isDay = false;
			sun.intensity = 0;
		} else if (currentTime >= dayLength) {
			currentTime = 0;
		float currentTimeF = currentTime;
		float dayLengthF = dayLength;
		earth.transform.eulerAngles =  new Vector3 (0, 0, (-(currentTimeF / dayLengthF) * 360)+90);

	IEnumerator TimeOfDay(){
		while (true) {
			currentTime += 1;
			int hours = Mathf.RoundToInt( currentTime / 60);
			int minutes = currentTime % 60;
			Debug.Log (hours+":"+minutes);
			yield return new WaitForSeconds(1F/cycleSpeed);

change color of background (with other spirit if u need) by code, use a time function, per 4 hour forexample change value of color " R + G + P, and not change any thing at alpha ( A )"
ofcource if u want add Lamp maybe u need to add anohter small spirit (that technique, for example if hous will ON lamp, the light show from outside, only add small spirit on the window that the light come out from it, not forger spirit to shadow (and player shadow) :slight_smile: )

good idea, make own system for light 2D XD, the idea is easy, the system only will add a new spirit with +1 layout to show it front of background spirit (maybe you need add another spirit to shadow, and make the new spirit same as part of Main spirit (background) because u need only light part of world, not all (like this), and sell the system on the assetS store XD

iam sorry mybe my english not good "any one can correct my comment :3 "