Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SynHawk · Jan 23, 2017 at 02:42 PM · 2dtimetransitionnightday

Unity2D Day and Night Cycle

Hi All,

I'm currently working on an individual project and I'm looking for the best way to implement a Day and Night Cycle into my game. So I'm going to give a brief description about my game:

It's a pixel 2D side-scroller survival sandbox RPG, similar to Terraria.

For my project, I want to add the feature of a Day and Night Cycle, to give the game a bit of an ambience, but I'm finding it increasingly difficult to implement it into a 2D game. I've searched through the forums for answers, but most, if not, all, are aimed at Unity3D projects. Telling you to change mesh colors and whatnot.

In my project, I've added Time, where 1 minute is 2 seconds real time. I'll post the code of my Time script below. In my Time script, I've implemented periods of the day, to help aid with the sky color.

 using UnityEngine;
 
 public class RelativeTime : MonoBehaviour
 {
     //    Variables
     TimeUI WorldTime;
 
     readonly float TimeDelay = 2.5F;
     int Hours, Minutes;
     float Timer;
 
     string HourValue, MinuteValue;
 
     //  Periods of the Day; in a 24-hour clock format
     const int Midnight = 0;      //  00:00 or 12:00 AM
     const int Dawn = 4;          //  04:00 or 04:00 AM
     const int Sunrise = 6;       //  06:00 or 06:00 AM
     const int Morning = 7;       //  07:00 or 07:00 AM
     const int Afternoon = 12;    //  12:00 or 12:00 PM
     const int Evening = 17;      //  17:00 or 05:00 PM
     const int Sunset = 18;       //  18:00 or 06:00 PM
     const int Dusk = 20;         //  20:00 or 08:00 PM
     const int Night = 21;        //  21:00 or 09:00 PM
 
     //  Max Minutes/Hours in a Day; also in a 24-hour clock format
     readonly int MaxMinutes = 59;
     readonly int MaxHours = 23;
 
     string DayPeriod;
 
     //    Methods
     void Awake()
     {
         WorldTime = GetComponent<TimeUI>();
         /*  
          * This area will be used to load in saved game data
          *  e.g. time of day, time format, etc.
          */
 
         #region Load Data
         
         #endregion
 
     }
 
     void Start()
     {
         updateTime();
         checkDayPeriod();
     }
 
     void LateUpdate()
     {
         Timer += Time.deltaTime;
         if (Timer >= TimeDelay)
         {
             ResetTimer(0.0F);
             IncreaseTime();
         }
     }
 
     void ResetTimer(float value)
     {
         Timer = value;
     }
 
     void IncreaseTime()
     {
         Minutes++;
         if (Minutes > MaxMinutes)
         {
             Minutes = 0;
             Hours++;
             checkDayPeriod();
             if (Hours > MaxHours)
             {
                 Hours = 0;
             }
         }
 
         WorldTime.updateTime(getTime(WorldTime.getClockType()));
     }
 
     void updateTime()
     {
         WorldTime.updateTime(getTime(WorldTime.getClockType()));
     }
 
     void checkDayPeriod()
     {
         switch (Hours)
         {
             case Midnight:
                 this.DayPeriod = "Midnight";
                 break;
             case Dawn:
                 this.DayPeriod = "Dawn";
                 break;
             case Sunrise:
                 this.DayPeriod = "Sunrise";
                 break;
             case Morning:
                 this.DayPeriod = "Morning";
                 break;
             case Afternoon:
                 this.DayPeriod = "Afternoon";
                 break;
             case Evening:
                 this.DayPeriod = "Evening";
                 break;
             case Sunset:
                 this.DayPeriod = "Sunset";
                 break;
             case Dusk:
                 this.DayPeriod = "Dusk";
                 break;
             case Night:
                 this.DayPeriod = "Night";
                 break;
             default:
                 break;
         }
 
         WorldTime.updateDayPeriod(DayPeriod);
     }
 
     public string getTime(bool twelveHour)
     {
         string timeOfDay, dayPeriod;
 
         //  Format the Minutes
         if (Minutes < 10)
         {
             MinuteValue = "0" + Minutes.ToString();
         }
         else
         {
             MinuteValue = Minutes.ToString();
         }
 
         //  Format the Hours
         if (Hours < 10)
         {
             HourValue = "0" + Hours.ToString();
         }
         else
         {
             HourValue = Hours.ToString();
         }
 
         //  Twelve-hour clock
         if (twelveHour == true)
         {
             if (Hours > Afternoon)
             {
                 //  Single digits should be displayed as 01...09, not 1...9
                 if ((Hours % Afternoon) > 9)
                 {
 
                     HourValue = (Hours % Afternoon).ToString();
                 }
                 else
                 {
                     HourValue = "0" + (Hours % Afternoon).ToString();
                 }
             }
             else if (Hours.Equals(0))
             {
                 HourValue = Afternoon.ToString();
             }
 
             //  Check period of day, i.e. AM or PM
             if (Hours >= Afternoon)
             {
                 dayPeriod = "PM";
             }
             else
             {
                 dayPeriod = "AM";
             }
 
             timeOfDay = HourValue + ":" + MinuteValue + " " + dayPeriod;
             return timeOfDay;
         }
         else
         {
             timeOfDay = HourValue + ":" + MinuteValue;
             return timeOfDay;
         }
     }
 }

I guess that's where my main question comes into things: is there any way I could change a sprite color using C# script, in accordance with the periods of the day? I'm looking into Color.Lerp, but changing color gradually e.g. from Sunrise to Morning. So, from Sunrise, the sky goes from a black sky to a dark orange, gradually getting brighter as time increments, but also changes to a blue as Morning approaches. Is this at all possible?

Any answers are welcomed!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to impliment a 10 minute day clock, and tie it to a directional light rotation. 4 Answers

Timed event Question 2 Answers

Time of day script not working 2 Answers

Is it possible to turn off/suppress event calls in an animation at runtime? 2 Answers

Parameterize the Animators transition condition (in 2D) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges