- Home /
Question by
glenohumeral13 · Jun 21, 2014 at 03:32 PM ·
daycycleburgzergarcade
How to start the day at day (Burgzerg Arcade)?
So if any of you are familiar with Burgzerg Arcade on YouTube (or are just good with code), hopefully you can figure out what my problem is. When I start the day, the day skyboxes are up, which is good. However, the sun isn't. Instead it comes up during the night.
using UnityEngine;
using System.Collections;
public class GameTime : MonoBehaviour {
public enum TimeOfDay {
Idle,
SunRise,
SunSet
}
public Transform[] sun;
private Sun[] _sunScript;
public float sunRise;
public float sunSet;
public float skyboxBlendModifier;
public float dayCycleInMinutes=1;
private const float SECOND = 1;
private const float MINUTE = 60 * SECOND;
private const float HOUR = 60 * MINUTE;
private const float DAY = 24 * HOUR;
private const float DEGREES_PER_SECOND = 360 / DAY;
private float _degreeRotation;
private float _timeOfDay;
private float _dayCycleInSeconds;
private TimeOfDay _tod;
private float _noonTime; //this is the time of day when it is noon
private float _morningLength;
private float _eveningLength;
// Use this for initialization
void Start () {
_tod = TimeOfDay.Idle;
_dayCycleInSeconds = dayCycleInMinutes * MINUTE;
RenderSettings.skybox.SetFloat("_Blend",0);
_sunScript = new Sun[sun.Length];
for (int cnt = 0; cnt < sun.Length; cnt++) {
Sun temp = sun[cnt].GetComponent<Sun>();
if (temp == null) {
Debug.LogWarning("Sun script not found");
sun[cnt].gameObject.AddComponent<Sun>();
temp = sun[cnt].GetComponent<Sun>();
}
_sunScript[cnt] = temp;
}
_timeOfDay = 0; //starts the day off at 0 seconds, original is 0
_degreeRotation = DEGREES_PER_SECOND * DAY / (_dayCycleInSeconds);
sunRise *= _dayCycleInSeconds;
sunSet *= _dayCycleInSeconds;
_noonTime = _dayCycleInSeconds / 2;
_morningLength = _noonTime - sunRise; //the length of the morning in seconds
_eveningLength = sunSet - _noonTime; //the length of the evening in seconds
SetupLighting (); //setup lighting to maxvalues on start
}
// Update is called once per frame
void Update () {
for(int cnt = 0; cnt < sun.Length; cnt ++)
sun [cnt].Rotate (new Vector3(_degreeRotation, 0, 0) * Time.deltaTime);
_timeOfDay += Time.deltaTime;
if (_timeOfDay > _dayCycleInSeconds)
_timeOfDay -= _dayCycleInSeconds;
// Debug.Log (_timeOfDay);
if (_timeOfDay > sunRise && _timeOfDay < _noonTime) {
AdjustLighting(true);
} else if(_timeOfDay > _noonTime && _timeOfDay < sunSet) {
AdjustLighting(false);
}
if (_timeOfDay > sunRise && _timeOfDay < sunSet && RenderSettings.skybox.GetFloat ("_Blend") < 1) {
_tod = GameTime.TimeOfDay.SunRise;
BlendSkybox ();
} else if (_timeOfDay > sunSet && RenderSettings.skybox.GetFloat ("_Blend") > 0) {
_tod = GameTime.TimeOfDay.SunSet;
BlendSkybox ();
} else {
_tod = GameTime.TimeOfDay.Idle;
}
}
private void BlendSkybox() {
float temp = 0;
switch (_tod) {
case TimeOfDay.SunRise:
temp = (_timeOfDay - sunRise) / _dayCycleInSeconds * skyboxBlendModifier;
break;
case TimeOfDay.SunSet:
temp = (_timeOfDay - sunSet) / _dayCycleInSeconds * skyboxBlendModifier;
temp = 1 - temp;
break;
}
RenderSettings.skybox.SetFloat("_Blend",temp);
Debug.Log (temp);
}
private void SetupLighting() {
for (int cnt = 0; cnt < _sunScript.Length; cnt++) {
if(_sunScript[cnt].giveLight){
sun[cnt].GetComponent<Light>().intensity = _sunScript[cnt].maxLightBrightness;
}
}
}
private void AdjustLighting (bool brighten) {
if (brighten) {
float pos = (_timeOfDay - sunRise) / _morningLength; // get the position of the sun in the morning sky
for (int cnt = 0; cnt < _sunScript.Length; cnt++) {
if (_sunScript [cnt].giveLight) {
_sunScript [cnt].GetComponent<Light> ().intensity = _sunScript [cnt].maxLightBrightness * pos;
}
}
}
else {
float pos = (sunSet - _timeOfDay) / _eveningLength; // get the position of the sun in the evening sky
for (int cnt = 0; cnt < _sunScript.Length; cnt++) {
if (_sunScript [cnt].giveLight) {
_sunScript [cnt].GetComponent<Light> ().intensity = _sunScript [cnt].maxLightBrightness * pos;
}
}
}
}
}
I know, it's really long, but it'd would be so great if someone could help me out. I have already tried editing the _timeOfDay variable in the Start function, as well as switch the skybox positions around.
Comment
Your answer
Follow this Question
Related Questions
BurgZergArcade Unity tutorial 19-20 errors. 0 Answers
DayNight Shadows Problems 0 Answers
How do you get the 24 Hour Time from rotation on the x axis? 1 Answer
Variable target in 'Targetting system' error. 1 Answer
Why is fading not smooth? 0 Answers