Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by mrchacko · Sep 07, 2020 at 10:28 PM · gametimemonodevelop

Unity - Trying to Calculate In-Game Time Relative to Sun/Moon Cycle

Hello,

I wrote a simple script for controlling a Sun/Moon object in my scene and that seems to be working although it's very primitive and has no shader to transition smoothly between skyboxes. That's irrelevant though.

I am now wracking my brain trying to figure out how to capture the current time of day per the game's circadian rhythm. That is I need to be able to launch other scripts in this off of the time of day relative to the in game time.

I have gotten it sorta working except I think an actual game second should be way faster than a real life second because we only have 120 seconds in a game day per my little experiment.

Here is my crappy code, any help is appreciated.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Text;
 using UnityEngine;
 
 public class DayNightCycle : MonoBehaviour
 {
     public static DayNightCycle instance;
 
     public Light sun, moon;
     public float secondsInFullDay = 3600f;
     
     [Range(0, 1)]
     public float currentTimeOfDay = 0f;
 
     [HideInInspector]
     public float timeMultiplier = 1f;
 
     float sunInitialIntensity;
 
     public Material skyboxDay, skyBoxNight;
 
     public float gameTime;
     public float currentSecond;
 
     public int minutes, hours, days;
     public int currentMinute, currentHour, currentDay;
     public float speedOfASecond;
 
     public float previousTime;
 
     //private float secondsOfARealDay = 24 * 60 * 60;
 
     private void Awake()
     {
         instance = this;
         
     }
     // Start is called before the first frame update
     void Start()
     {
         sunInitialIntensity = sun.intensity;
         currentSecond = 0f;
     }
 
     // Update is called once per frame
     void Update()
     {
         UpdateSun();
 
         currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;
 
         if (currentTimeOfDay >= 1)
         {
             currentTimeOfDay = 0;
         }
 
         // seconds / total seconds = percentage
         // percentage * seconds = total seconds
         // seconds = total seconds * percentage
 
         currentSecond = (secondsInFullDay * currentTimeOfDay);
 
         if (currentSecond >= 60)
             currentMinute = (int)(currentSecond % 60);
 
         if (currentMinute >= 60)
             currentHour = (int)(currentMinute % 60);
 
         if (currentHour >= 24)
             IncrementDays();
 
         previousTime = (int)gameTime;
 
     }
 
     void UpdateSun()
     {
         sun.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 90, 170, 0);
         moon.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 90, 170, 0);
 
         float intensityMultiplier = 1f;
         float moonIntensityMult = 0.025f;
 
         if (currentTimeOfDay <= 0.23f || currentTimeOfDay >= 0.75f)
         {
             RenderSettings.skybox = skyBoxNight;
             intensityMultiplier = 0f;
             moonIntensityMult = 0.025f;
         }
         else if (currentTimeOfDay <= 0.25f)
         {
             RenderSettings.skybox = skyBoxNight;
             intensityMultiplier = Mathf.Clamp01((currentTimeOfDay - 0.23f) * (1 / 0.02f));
             moonIntensityMult = 0f;
         }
         else if (currentTimeOfDay >= 0.73f)
         {
             RenderSettings.skybox = skyboxDay;
             
             intensityMultiplier = Mathf.Clamp01(1 - ((currentTimeOfDay - 0.73f) * (1 / 0.02f)));
             moonIntensityMult = 0f;
         }
 
         sun.intensity = sunInitialIntensity * intensityMultiplier;
         moon.intensity = moonIntensityMult;
     }
 
     public float GetTimeOfDayInSeconds
     {
         get { return currentTimeOfDay; }
         set { return; }
     }
 
     void IncrementDays()
     {
         currentDay++;
         currentSecond = 0;
         currentMinute = 0;
         currentHour = 0;
     }
 
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by mrchacko · Sep 08, 2020 at 12:54 AM

Okay I actually just figured out what I was doing wrong and here is the code for a full 24 hour cycle.

Note** This is a very sloppy version. The longer you want to handle how long a game day is the more broken this is going to get (because of the day increment, I wasn't sure how to do this smoothly before the currentTimeOfDay returned to 0). It works for like 3600 seconds of real time being a full 24 hours of game time.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Text;
 using UnityEngine;
 
 public class DayNightCycle : MonoBehaviour
 {
     public static DayNightCycle instance;
 
     public Light sun, moon;
     public float secondsInFullDay = 3600f;
     
     [Range(0, 1)]
     public float currentTimeOfDay = 0f;
 
     [HideInInspector]
     public float timeMultiplier = 1f;
 
     float sunInitialIntensity;
 
     public Material skyboxDay, skyBoxNight;
 
     public double elapsedRealTime;
 
     public float secondsPerDayMultiplier;
 
     [SerializeField]
     private float speedOfGameSecond, speedOfGameMinute, speedOfGameHour, speedOfGameDay;
 
     [SerializeField]
     private float currentGameSecond, currentGameMinute, currentGameHour, currentGameDay;
 
     private bool stopIncrementingDay;
 
     private void Awake()
     {
         instance = this;
     }
     // Start is called before the first frame update
     void Start()
     {
         speedOfGameSecond = secondsInFullDay / 24 / 60 / 60;
         speedOfGameMinute = secondsInFullDay / 24 / 60;
         speedOfGameHour = secondsInFullDay / 24;
         speedOfGameDay = secondsInFullDay;
 
         
         sunInitialIntensity = sun.intensity;
     }
 
     // Update is called once per frame
     void Update()
     {
         UpdateSun();
 
         currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;
 
         if (currentTimeOfDay >= 1)
         {
             currentTimeOfDay = 0;
         }
 
         secondsPerDayMultiplier = currentTimeOfDay * secondsInFullDay;
 
         // seconds / total seconds = percentage
         // percentage * seconds = total seconds
         // seconds = total seconds * percentage
 
         currentGameSecond = secondsPerDayMultiplier / speedOfGameSecond;
 
         currentGameMinute = secondsPerDayMultiplier / speedOfGameMinute;
        
         currentGameHour = secondsPerDayMultiplier / speedOfGameHour;
 
         if(!stopIncrementingDay && currentGameHour >= 23.999)
         {
             IncrementDay();
             stopIncrementingDay = true;
         } else if(currentGameHour <= 23.999)
         {
             stopIncrementingDay = false;
         }
 
         elapsedRealTime += Time.deltaTime;
         previousTime = Time.deltaTime;
 
     }
 
     void UpdateSun()
     {
         sun.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 90, 170, 0);
         moon.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 90, 170, 0);
 
         float intensityMultiplier = 1f;
         float moonIntensityMult = 0.025f;
 
         if (currentTimeOfDay <= 0.23f || currentTimeOfDay >= 0.85f)
         {
             RenderSettings.skybox = skyBoxNight;
             intensityMultiplier = 0f;
             moonIntensityMult = 0.025f;
         }
         else if (currentTimeOfDay <= 0.25f)
         {
             RenderSettings.skybox = skyBoxNight;
             intensityMultiplier = Mathf.Clamp01((currentTimeOfDay - 0.23f) * (1 / 0.02f));
             moonIntensityMult = 0f;
         }
         else if (currentTimeOfDay >= 0.83f)
         {
             RenderSettings.skybox = skyboxDay;
             
             intensityMultiplier = Mathf.Clamp01(1 - ((currentTimeOfDay - 0.83f) * (1 / 0.02f)));
             moonIntensityMult = 0f;
         }
 
         sun.intensity = sunInitialIntensity * intensityMultiplier;
         moon.intensity = moonIntensityMult;
     }
 
     public float GetTimeOfDayPercent
     {
         get { return currentTimeOfDay; }
         set { return; }
     }
 
     public float GetSecondsPerDay()
     {
         return secondsInFullDay;
     }
 
     private void IncrementDay()
     {
         currentGameSecond = 0;
         currentGameMinute = 0;
         currentGameHour = 0;
         currentGameDay++;
     }
 
     public void GetTimeOfDay()
     {
         // now to work on this
     }
 }
 

Comment
Add comment · Share
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
avatar image
1

Answer by Maniacbob · Sep 07, 2020 at 11:34 PM

It might not exactly answer your question but a good starting point might be to look at UnityEvents. You could use an event tied to a specific time to trigger an event or action.

I'd probably create a class to inherit from that has three properties, an update function, and an event function. The first property would be the day for the trigger to occur on, the second would be a time format for when the trigger would go off that you could either tie directly to currentTimeofDay or break it out into hours, minutes, and seconds, and the third would be the event. The update function would listen to the day/night cycle and compare it to its time property and if the time has arrived to invoke its event. The event function would then be blank such that each implemented event could override it to follow its own unique event.

Something like that.

If you don't want to run each one independently, then you could forgo the update loop and add all the events to a List and check each one every update (or some subsection of time). Loop through the list and check to see if the time has passed the event and if so, then invoke the event.

Comment
Add comment · Show 2 · Share
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
avatar image mrchacko · Sep 07, 2020 at 11:45 PM 0
Share

I could go that route but I'd also like to be able to display the current in game time on clocks in the world.

I'm really just not sure how to compute the in game time once I can figure that out...

avatar image mrchacko · Sep 07, 2020 at 11:52 PM 0
Share

I think I just solved it using a datetime object. Nope never$$anonymous$$d.

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

248 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

"Field " " is never assigned to, and will always have it's default value null" 2 Answers

Pause scene?!? 1 Answer

C# How to have a scripted if statement last x amount of seconds 1 Answer

Is this a bug in MonoDevelop? I do NOT see what is wrong 2 Answers

error CS1041: Identifier expected 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