Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How would one go about tracking the days in game?(I have some ideas butt)

Discussion in 'Scripting' started by GigiB, Sep 13, 2014.

  1. GigiB

    GigiB

    Joined:
    Sep 5, 2014
    Posts:
    14
    I was wondering how to make the game start tracking days in game. Basically adding Mon-Sun. Also adding Months and years.

    As of now I have A day and night script. Heres what I was thinking I can make an Enumeration:

    Code (CSharp):
    1.     public enum Months
    2.     {
    3.         January,
    4.         February,
    5.         March,
    6.         April,
    7.         May,
    8.         June,
    9.         July,
    10.         August,
    11.         September,
    12.         October,
    13.         November,
    14.         December
    15.     };
    16.  
    17.     public enum Days
    18.     {
    19.         Monday,
    20.         Tuesday,
    21.         Wednesday,
    22.         Thursday,
    23.         Friday,
    24.         Saturday
    25.     };
    Then I use my GameTime Script to create an if statement (Maybe), that is where I got lost. How do I actually implement the enum in-order to make the day change.
     
  2. DogOriginal

    DogOriginal

    Joined:
    Sep 3, 2014
    Posts:
    14
    Just do an integer that gets +1'd every time a day comes to an end. Can't help more there as I don't see the code.

    Then do gui.label(new Rect(50,50,50,50), dayCount.ToString);

    Also, this is C# not C++, no need for the };

    Ignore what I said, I thought you meant like "Days Passed: 50" or something.

    Okay, so tracking a date/time, you could just output several well positioned labels, Day/Month/Year.

    Then you do this

    Code (CSharp):
    1. public string Day = "";
    2.  
    3. public string Monday = "Monday";
    4.  
    5. Public int dayCount = 0;
    6.  
    7. guilabel - dayLabel
    8.  
    9. //monday
    10. if(dayCount = 1){
    11. dayLabel = Monday
    12. }
    13.  
    14. That'll output monday as day 1 of the week, that's the general idea, I just the same thing for my labels to get choices output to them and for some reason this tiny box is causing my brain to go blank so I'm giving up :P
     
    Last edited: Sep 13, 2014
  3. GigiB

    GigiB

    Joined:
    Sep 5, 2014
    Posts:
    14
    We this be correct
    Would you use a loop to implement the +1 effect.

    Here's the game time code BTW:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System;
    5.  
    6. public class GameTime : MonoBehaviour {
    7.     public enum TimeOfDay
    8.         {
    9.         Idle,
    10.         SunRise,
    11.         SunSet,
    12.         }
    13.     public Transform[] sun;
    14.     public float dayCycleInMinutes = 1;    //Tells of how many IRL minutes is a whole day in game
    15.  
    16.     public float sunRise;                   //the time of day that we start the sunrise
    17.     public float sunSet;                    // The time of day that we start the sun set
    18.     public float skyboxBlendModifier;       //The speed at which the textures in the skybox blend
    19.  
    20.     private Sun[] _sunScript;
    21.     private float _degreeRotation;
    22.     private float _timeOfDay;
    23.  
    24.  
    25.     private float _dayCycleInSeconds;
    26.  
    27.     private const float SECOND = 1;
    28.     private const float MINUTE = 60 * SECOND;
    29.     private const float HOUR = 60 * MINUTE;
    30.     private const float DAY = 24* HOUR;
    31.     private const float DEGREES_PER_SECOND = 360 / DAY;     // Sun goes 360 means One full day
    32.  
    33.     private TimeOfDay _tod;
    34.  
    35.  
    36.  
    37.     private float gameHour;
    38.  
    39.  
    40.     void Awake()
    41.     {
    42.  
    43.     }
    44.  
    45.     #region
    46.     // Use this for initialization
    47.     void Start ()
    48.     {
    49.  
    50.         _tod = TimeOfDay.Idle;
    51.         _dayCycleInSeconds = dayCycleInMinutes * MINUTE;
    52.  
    53.         RenderSettings.skybox.SetFloat ("_Blend", 0);
    54.  
    55.         _sunScript = new Sun[sun.Length];
    56.         for (int cnt = 0; cnt < sun.Length; cnt++)
    57.         {
    58.         Sun temp = sun[cnt].GetComponent<Sun>();
    59.  
    60.             if(temp == null)
    61.             {
    62.                 Debug.LogWarning("Sun script not found. Adding it");
    63.                 sun[cnt].gameObject.AddComponent<Sun>();
    64.                 temp = sun[cnt].GetComponent<Sun>();
    65.             }
    66.             _sunScript[cnt] = temp;
    67.             sunRise *= _dayCycleInSeconds;
    68.             sunSet *= _dayCycleInSeconds;
    69.         }
    70.  
    71.  
    72.         _timeOfDay = 0;
    73.         _degreeRotation = DEGREES_PER_SECOND * DAY / (_dayCycleInSeconds);
    74.  
    75.  
    76.     }
    77.     #endregion
    78.  
    79.     // Update is called once per frame
    80.     void Update () {
    81.  
    82.         DaysPassed ();
    83.  
    84.         for(int cnt = 0; cnt< sun.Length; cnt++)
    85.         sun[cnt].Rotate(new Vector3(_degreeRotation, 0, 0) * Time.deltaTime);
    86.  
    87.         _timeOfDay += Time.deltaTime;
    88.  
    89.         if (_timeOfDay > _dayCycleInSeconds)
    90.                 _timeOfDay -= _dayCycleInSeconds;
    91.  
    92.         //Debug.Log (_timeOfDay);
    93.  
    94.         if (_timeOfDay > sunRise && _timeOfDay < sunSet && RenderSettings.skybox.GetFloat ("_Blend") < 1)
    95.         {
    96.                         _tod = GameTime.TimeOfDay.SunRise;
    97.                         BlendedSkybox ();
    98.         }
    99.         else if (_timeOfDay > sunSet && RenderSettings.skybox.GetFloat ("_Blend") > 0)
    100.         {
    101.                         _tod = GameTime.TimeOfDay.SunSet;
    102.                         BlendedSkybox ();
    103.  
    104.         }
    105.         else
    106.         {
    107.             _tod = GameTime.TimeOfDay.Idle;
    108.         }
    109.     }
    110.  
    111.     private void BlendedSkybox()       //Handels transition og day and night skyboxes
    112.     {
    113.         float temp = 0;
    114.  
    115.         switch (_tod)
    116.         {
    117.         case TimeOfDay.SunRise:
    118.             temp = (_timeOfDay - sunRise) / _dayCycleInSeconds * skyboxBlendModifier;
    119.             break;
    120.         case TimeOfDay.SunSet:
    121.             temp = (_timeOfDay - sunSet) / _dayCycleInSeconds * skyboxBlendModifier;
    122.             temp = 1 - temp;
    123.             break;
    124.         }
    125.  
    126.  
    127.         RenderSettings.skybox.SetFloat("_Blend", temp);
    128.  
    129.         //Debug.Log (temp);
    130.  
    131.     }
    132.  
    133.     private float DaysPassed ()
    134.     {
    135.         gameHour = _dayCycleInSeconds / 24f;
    136.  
    137.         if (gameHour == _dayCycleInSeconds) {
    138.                         Debug.Log (gameHour);
    139.                         Debug.Log ("24 Hours have passed");
    140.                 } else
    141.                         Debug.Log ("Not working ");
    142.         return (gameHour);
    143.  
    144.          }
    145.  
    146.  
    147. }
    Most of it I got from a tutorial.
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Instead of tracking the time yourself, you could use the DateTime and TimeSpan structs. You still have to calculate sunsets, sun position, time of day yourself, but you might be able to simplify some things with those structs.
     
  5. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    Code (CSharp):
    1. //GameTime.cs
    2. //C#
    3. using UnityEngine;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using System;
    7. public class GameTime : MonoBehaviour
    8. {
    9.    public enum TimeOfDay
    10.    {
    11.      Idle,
    12.      SunRise,
    13.      SunSet,
    14.    }
    15.    
    16.    public enum Months
    17.   {
    18.   January,
    19.   February,
    20.   March,
    21.   April,
    22.   May,
    23.   June,
    24.   July,
    25.   August,
    26.   September,
    27.   October,
    28.   November,
    29.   December
    30.   };
    31.   public enum Days
    32.   {
    33.   Monday,
    34.   Tuesday,
    35.   Wednesday,
    36.   Thursday,
    37.   Friday,
    38.   Saturday
    39.   };
    40.    
    41.    public Transform[] sun;
    42.    public float dayCycleInMinutes = 1;  //Tells of how many IRL minutes is a whole day in game
    43.    public float sunRise;  //the time of day that we start the sunrise
    44.    public float sunSet;  // The time of day that we start the sun set
    45.    public float skyboxBlendModifier;  //The speed at which the textures in the skybox blend
    46.    private Sun[] _sunScript;
    47.    private float _degreeRotation;
    48.    private float _timeOfDay;
    49.    private float _dayCycleInSeconds;
    50.    private const float SECOND = 1;
    51.    private const float MINUTE = 60 * SECOND;
    52.    private const float HOUR = 60 * MINUTE;
    53.    private const float DAY = 24* HOUR;
    54.    private const float DEGREES_PER_SECOND = 360 / DAY;  // Sun goes 360 means One full day
    55.    private TimeOfDay _tod;
    56.    private float gameHour;
    57.    private int gameDay;   //added to track days
    58.    void Awake()
    59.    {
    60.    }
    61.    
    62.    void OnGUI()   //added
    63.    {//prints out the current day, month and year in-game
    64.      GUI.Label(new Rect(0, 0, Screen.width, 25), String.Format("Game Day: {0} = {1}, Game Month: {2}, GameYear: {3}",gameDay,GetDay(),GetMonth(),GetYear());
    65.    }
    66.    
    67.    #region
    68.    // Use this for initialization
    69.    void Start()
    70.    {
    71.      gameDay = 0;   //added
    72.      
    73.      _tod = TimeOfDay.Idle;
    74.      _dayCycleInSeconds = dayCycleInMinutes * MINUTE;
    75.      RenderSettings.skybox.SetFloat("_Blend", 0);
    76.      _sunScript = new Sun[sun.Length];
    77.      for (int cnt = 0; cnt < sun.Length; cnt++)
    78.      {
    79.        Sun temp = sun[cnt].GetComponent<Sun>();
    80.        if(temp == null)
    81.        {
    82.          Debug.LogWarning("Sun script not found. Adding it");
    83.          sun[cnt].gameObject.AddComponent<Sun>();
    84.          temp = sun[cnt].GetComponent<Sun>();
    85.        }
    86.        _sunScript[cnt] = temp;
    87.        sunRise *= _dayCycleInSeconds;
    88.        sunSet *= _dayCycleInSeconds;
    89.      }
    90.      _timeOfDay = 0;
    91.      _degreeRotation = DEGREES_PER_SECOND * DAY / (_dayCycleInSeconds);
    92.    }
    93.    #endregion
    94.    // Update is called once per frame
    95.    void Update()
    96.    {
    97.      for(int cnt = 0; cnt< sun.Length; cnt++)
    98.        sun[cnt].Rotate(new Vector3(_degreeRotation, 0, 0) * Time.deltaTime);
    99.      _timeOfDay += Time.deltaTime;
    100.      if (_timeOfDay >= _dayCycleInSeconds)
    101.      {
    102.        _timeOfDay -= _dayCycleInSeconds;
    103.        gameDay++;   //added
    104.      }
    105.      if (_timeOfDay > sunRise && _timeOfDay < sunSet && RenderSettings.skybox.GetFloat("_Blend") < 1)
    106.      {
    107.        _tod = GameTime.TimeOfDay.SunRise;
    108.        BlendedSkybox();
    109.      }
    110.      else if (_timeOfDay > sunSet && RenderSettings.skybox.GetFloat("_Blend") > 0)
    111.      {
    112.        _tod = GameTime.TimeOfDay.SunSet;
    113.        BlendedSkybox();
    114.      }
    115.      else
    116.      {
    117.        _tod = GameTime.TimeOfDay.Idle;
    118.      }
    119.    }
    120.    private void BlendedSkybox()  //Handels transition og day and night skyboxes
    121.    {
    122.      float temp = 0;
    123.      switch (_tod)
    124.      {
    125.        case TimeOfDay.SunRise:
    126.          temp = (_timeOfDay - sunRise) / _dayCycleInSeconds * skyboxBlendModifier;
    127.          break;
    128.        case TimeOfDay.SunSet:
    129.          temp = (_timeOfDay - sunSet) / _dayCycleInSeconds * skyboxBlendModifier;
    130.          temp = 1 - temp;
    131.          break;
    132.      }
    133.      RenderSettings.skybox.SetFloat("_Blend", temp);
    134.    }
    135.    
    136.    //ADDED FUNCTIONS BELOW
    137.    
    138.    public string GetDay()
    139.    {//returns the exact day of week assuming the very first day is always Monday
    140.      return ((Days)(gameDay%7)).ToString();
    141.    }
    142.    
    143.    public string GetMonth()
    144.    {//returns approximate month
    145.      return ((Months)((gameDay%365)/30.416667f)).ToString();
    146.    }
    147.    
    148.    public int GetYear()
    149.    {//returns approximate year
    150.      return (int)Mathf.Ceil(gameDay/365.0f);
    151.    }
    152. }
    153.  
    This is my brief attempt, I hope it helps or at least enlightens you some.