- Home /
Time of day script not working
I made a script that makes the time of day change gradually at a certain speed, but I can't get the sky color to change the right way. The variable "sky" in this script is the same material that the skybox is. The color of the sky is supposed to gradually change color and not change immediately. This is the script:
var speed : float = 0.5;
var stars : GameObject;
var day = Color(.302, .404, .808, 1);
var night = Color.black;
private var sunset : boolean;
var mid = Color.red;
var sky : Material;
var curSky : Color;
var smooth : float;
function Update() {
transform.Rotate(speed*Time.deltaTime,0,0);
if(transform.rotation.eulerAngles.x < 30){
if(sunset == true){
curSky = Color.Lerp(day, mid, Time.deltaTime * smooth);
}
if(sunset == false){
curSky = Color.Lerp(night, mid, Time.deltaTime * smooth);
}
}
if(transform.rotation.eulerAngles.x > 30){
if(transform.rotation.eulerAngles.x < 90){
sunset = true;
curSky = Color.Lerp(mid, day, Time.deltaTime * smooth);
}
}
if(transform.rotation.eulerAngles.x > 90){
sunset = false;
curSky = Color.Lerp(mid, night, Time.deltaTime * smooth);
}
sky.color = curSky;
}
It will help you to understand the problem not to solve it completely. http://answers.unity3d.com/questions/14288/can-someone-explain-how-using-timedeltatime-as-t-i.html
Answer by Vonni · Jan 02, 2014 at 12:33 AM
Think you are misunderstand how lerp works.
Time.deltaTime * smooth
will be roughly the same value each frame.
Color.Lerp(Black, White, 0) // will return black
Color.Lerp(Black, White, 1) // will return white
Color.Lerp(Black, White, 0.5) // will return gray
So you should include Time.time somewhere in yourscript
Thanks for the advice. Time.time works great. I understood how lerp works, it's that I haven't really used Time too much. If anyone else has the same problem, I'm going to put my final script below:
var stars : GameObject;
var day = Color(.302, .404, .808, 1);
var night = Color.black;
private var sunset : boolean;
var mid = Color.red;
var sky : $$anonymous$$aterial;
var curSky : Color;
var smooth : float;
var curTime = 0.0;
var timer = 1;
private var days = 0;
private var taken = 0.0;
InvokeRepeating("TimeReset", (360/speed), (360/speed));
function Update() {
transform.Rotate(speed*Time.deltaTime,0,0);
if(transform.rotation.eulerAngles.x < 30){
if(sunset == true){
timer += smooth * Time.deltaTime;
curSky = Color.Lerp(day, mid, smooth*(curTime - (150/speed)));
}
if(sunset == false){
timer += smooth * Time.deltaTime;
curSky = Color.Lerp(night, mid, smooth*(curTime - (0/speed)));
stars.SetActive(true);
}
}
if(transform.rotation.eulerAngles.x > 30){
if(transform.rotation.eulerAngles.x < 90){
timer += smooth * Time.deltaTime;
sunset = true;
curSky = Color.Lerp(mid, day, smooth*(curTime - (30/speed)));
}
}
if(transform.rotation.eulerAngles.x > 90){
timer += smooth * Time.deltaTime;
sunset = false;
curSky = Color.Lerp(mid, night, smooth*(curTime - (180/speed)));
stars.SetActive(true);
}
sky.color = curSky;
if(timer > 1){
timer = 1;
}
curTime = (Time.time) - taken;
}
function TimeReset(){
taken = taken + Time.time;
days = days + 1;
}
FYI, the timer variable does nothing.
Answer by White-Chocolate-Development · Nov 25, 2015 at 12:19 PM
using UnityEngine; using System.Collections;
public class TimeOfDay : MonoBehaviour {
//This is just using the default sky-box only if the sky-box is set to procedural
public float time = 0.5f;
void Update () {
this.transform.Rotate (Vector3.right * time * Time.smoothDeltaTime);
}
}
Your answer
Follow this Question
Related Questions
Unity2D Day and Night Cycle 0 Answers
referring to a particular material 0 Answers
Make a change every 0.02 seconds precise 1 Answer
Time elapsed between variable change? 1 Answer
Sliding between camera change 1 Answer