- Home /
day/night cycle runs only twice
as my title says my day/night cycle only runs twice. i cant really understand why, because i have a clock that says when iGameTime is over 225 then reset to 0.
here is the script:
using UnityEngine;
using System.Collections;
public class DayNight : MonoBehaviour {
public float iGameTime;
public float rotatingSpeed = 8f;
public Camera mainCamera;
public int gameTimeSpeed = 1;
public Vector4 Skycolor;
public bool dawn;
public bool day;
public bool dusk;
public bool night;
// Use this for initialization
void Start () {
dawn = false;
day = false;
dusk = false;
night = false;
}
// Update is called once per frame
void Update () {
iGameTime = Time.time * gameTimeSpeed;
if (iGameTime > 225)
iGameTime -= 225;
transform.Rotate (new Vector3 (-0.2f, 0, 0) * (Time.deltaTime * rotatingSpeed));
mainCamera.backgroundColor = Skycolor;
// Dawn
if (iGameTime > 0 && iGameTime <= 56){
Skycolor = new Vector4 (0.23f + (0.009f * iGameTime), 0.26f + (0.005f * iGameTime), 0.55f, 0f);
dawn = true;
day = false;
dusk = false;
night = false;
}
// Day
else if (iGameTime > 56 && iGameTime <= 112){
if (iGameTime > 56 && iGameTime <= 87){
Skycolor = new Vector4 (0.7339369f - 0.0005f * (iGameTime - 56), 0.539965f - 0.005f * (iGameTime - 56), 0.55f, 0);
} else if (iGameTime > 87 && iGameTime <= 112){
Skycolor = new Vector4 (0.718458f - 0.005f * (iGameTime - 87), 0.3851757f - 0.009f * (iGameTime - 87), 0.55f, 0);
}
dawn = false;
day = true;
dusk = false;
night = false;
}
// Dusk
else if (iGameTime > 112 && iGameTime <= 169){
Skycolor = new Vector4 (0.5937353f - 0.006f * (iGameTime - 112), 0.1606749f - 0.012f * (iGameTime - 112), 0.55f, 0);
dawn = false;
day = false;
dusk = true;
night = false;
}
// Night
else if (iGameTime > 169 && iGameTime <= 225){
Skycolor = new Vector4 (0.2521621f - 0.000496f * (iGameTime - 169), -0.5224715f + 0.0142f * (iGameTime - 169), 0.55f, 0);
dawn = false;
day = false;
dusk = false;
night = true;
}
}
}
Answer by fafase · Nov 06, 2013 at 03:14 PM
First off you should use enum instead of 4 boolean.
enum State{Dawn,Day, Dusk, Night}
State state= State.Dawn;
Then the reason you only get two cycles is because you use Time.time. First round your variable gets to 225 then you subtract 225 to it but Time.time continue increasing.
You'd rather use:
iGameTime = Time.deltaTime* gameTimeSpeed;
if (iGameTime > 225)
iGameTime = 0;
Concerning the color of your skybox you might as well use Color.Lerp. That will change it gradually instead of the abrupt change you probably get now.
so enums works like booleans?
Thanks for the explicatory answer!
Actually, the change of colors runs pretty smoothly after a long while of testing ^^ but i will definetly check Color.Lerp out! Again thanks!
Enum are not boolean and do not work the same. They just allow you to define a list of state into one. Like I did. Then ins$$anonymous$$d of constantly having four lines to set all boolean to true or false, you only have one line:
state = State.chosenState;
then you can check using:
if(state == State.Night){}
Your answer
Follow this Question
Related Questions
Day Night Cycle sun fade 1 Answer
I can't get my script to work I get an error. [Please Help!] 0 Answers
How to make a Day/Night Cycle? 2 Answers
Photon Day Night Cycle Unsynced 1 Answer
Blend two lightmaps for day/night cycle? (terrain) 0 Answers