spenibus.net
2014-04-09 17:49:53 GMT

A simple day night cycle, part 2 - Time, initialisation and phases conditions.

Step 3: Time -- We need a time system in the game world to which to synchronise. To keep it simple, we increment a float value either at "Update()" or "FixedUpdate()" with the value of "Time.deltaTime". We can use an actual timestamp to simulate real dates or simply start at day 1 00h00m00s. In this example, we will use the latter. I will also remind you there are 86400 seconds in a day and 3600 seconds in an hour. I consider the time system should be implemented separately and offer plenty of variables to know its current status. Here is a brief overview of such a system: Update 2014-04-09 19:58 +0000 -- ```` double time; time += Time.deltaTime; // during Update() or FixedUpdate() int day = (int)_time / 86400 + 1; int hour = (int)_time % 86400 / 3600; int minute = (int)_time % 86400 % 3600 / 60; int second = (int)_time % 86400 % 3600 % 60; ```` This can be optimised but that is not the focus of this series of articles. Original text -- ```` float time += Time.deltaTime; // during Update() or FixedUpdate() int day = (int)Mathf.Floor(time / 86400 + 1); // starts at 1 int hour = (int)Mathf.Floor(time % 86400 / 3600); // 0-23 int minute = (int)Mathf.Floor(time % 86400 % 3600 / 60); // 0-59 int second = (int)Mathf.Floor(time % 86400 % 3600 % 60); // 0-59 ```` Step 4: Initialisation -- >updated 2014-04-09 19:58 +0000 Configuration: ```` timeLogic timeLogic; Transform earthCentre; Transform sun; Transform moon; // cameras Transform playerCamera; Transform cam; Vector3 sunPosStart; #csharp Then in Start(), we grab all the references we need for later use: #csharp: // this is the time system timeLogic = GameObject.Find("gameLogic").GetComponent<timeLogic>(); playerCamera = GameObject.Find("camMain").transform; cam = transform.Find("camera"); earthCentre = transform.Find("earthCentre"); sun = transform.Find("earthCentre/sun"); moon = transform.Find("moon"); sunPosStart = sun.position; ```` "sunPosStart" is the position of the sun at midnight. It is set in the editor but could be set via a Vector3 position in the configuration as well. Note that you can make use of the keyword "public" and simply drag gameobjects in the editor to have access to them instead of using "Find()", I'm just more code oriented. Step 5: Phases conditions -- This is simple, inside Update() we split the day in the phases outlined earlier: ```` // night is default // sunrise, part 1 if(timeLogic.hour >= 6 && timeLogic.hour < 7) { } // sunrise, part 2 else if(timeLogic.hour >= 7 && timeLogic.hour < 8) { } // day else if(timeLogic.hour >= 8 && timeLogic.hour < 20) { } // sunset, part 1 else if(timeLogic.hour >= 20 && timeLogic.hour < 21) { } // sunset, part 2 else if(timeLogic.hour >= 21 && timeLogic.hour < 22) { } ```` Sunrise and sunset are split in 2 parts for reasons that will be explained as we go. We initialise variables with values defaulting to the night phase, which is why we don't use an else condition. And this is the end of part 2. Then comes [part 3](http://spenibus.net/b/p/4/A-simple-day-night-cycle-part-3-Some-configuration-preparing-the-loop-night-and-sunrise).