hello everyone,
am trying to make a day-night cycle by lerping the color of a the material from blue to black and also i have an emptyObject that contain 2 spheres (sun & moon) sun is in (0,10,0) and moon is in (0,-10,0) (that emptyObject is a child of the camera)
now the thing is that i managed to make it work for the color by setting the lerp time(t) to this :
t += Time.deltaTime / duration;
and i thought that the correct way to do this for the sun and moon object is to make
Speed = Distance / duration ;
but it never worked, am sure that this is the right formula but the variable “Distance” and “Duration” am not sure that am entering them right,
notice that in order to make a full day cycle, the object should rotate 180 degree in it’s Z axis (maybe this could help)
thank you
@meat5000
thank you that save me a lot of time, the sun cycle is not working perfectly, but the color lerp is not perfectly synchronized
code :
sun Slerp :
using UnityEngine;
using System.Collections;
public class sunSet : MonoBehaviour
{
//sunrise position
public Transform sunrise;
//sunset position
public Transform sunset;
//total time
public float journeyTime = 1.0f ;
//counter
private float startTime;
//materials
public Material sun, moon;
void Start ()
{
//init
startTime = Time.time;
gameObject.renderer.material = sun;
}
void Update ()
{
journey ();
}
void journey ()
{
//Slerp code from unity script doc
Vector3 center = (sunrise.position + sunset.position) * 0.5F;
center -= new Vector3 (0, 1, 0);
Vector3 riseRelCenter = sunrise.position - center;
Vector3 setRelCenter = sunset.position - center;
float fracComplete = (Time.time - startTime) / journeyTime;
transform.position = Vector3.Slerp (riseRelCenter, setRelCenter, fracComplete);
transform.position += center;
//loop the cycle and change material
if (transform.position == sunset.position) {
transform.position = sunrise.position;
startTime = Time.time;
if (gameObject.renderer.material.name == "sun (Instance)") {
gameObject.renderer.material = moon;
} else {
gameObject.renderer.material = sun;
}
}
}
}
SkyColor lerp :
using UnityEngine;
using System.Collections;
public class SkyTrans : MonoBehaviour
{
//day & night colors
public Color day = new Color (0.01f, 0.4f, 0.8f);
public Color night = new Color (0.01f, 0.01f, 0.02f);
//main color
public Color skyColor ;
//total time
public float journeyTime;
//lerp speed
public float lerpSpeed ;
//checking if it's day or night
bool isDay ;
//instance of the sun object
sunSet sun;
void Start ()
{
//init
sun = GameObject.Find ("sun").GetComponent<sunSet> ();
lerpSpeed = 0;
journeyTime = sun.journeyTime;
skyColor = day;
isDay = true;
}
void Update ()
{
//setting the color to skyColor
gameObject.renderer.material.color = skyColor;
if (lerpSpeed < 1) {
lerpSpeed += Time.deltaTime / journeyTime;
}
//if it's day then lerp to night
if (isDay) {
skyColor = Color.Lerp (day, night, lerpSpeed / 2);
}
//if it's night then lerp to day
else {
skyColor = Color.Lerp (night, day, lerpSpeed / 2);
}
//day or night
if (skyColor == day) {
isDay = true;
lerpSpeed = 0;
} else if (skyColor == night) {
isDay = false;
lerpSpeed = 0;
}
}
}
thank you
You should post your script! Anyway, I suppose that both things must cycle from day to night and night to day - am I right? If so, the empty could be Rotate’d at the right speed, while the color should Lerp from blue to black during the sunset, then back to blue during the dawn. My suggestion is to control both with the same variable in order to avoid loss of synchronicity over time. Basically, the code could be something like this:
public var sunMoon: Transform; // drag here the empty that contains sun and moon
public var duration: float = 3600; // day duration in seconds (1 hour, in this example)
public var transitionTime: float = 0.2; // transition duration control
public var nightColor = Color.black;
public var dayColor = Color.blue;
private var curTime: float = 0;
function Update(){
curTime += Time.deltaTime; // update curTime
var sunAngle: float = 360*curTime/duration; // calculate rotation angle
sunMoon.eulerAngles = Vector3(0,0,sunAngle); // set rotation angle about Z
// transition goes 0->1 when sunAngle near to zero, 1->0 when around 180
var transition: float = Mathf.Sin(sunAngle*Mathf.Deg2Rad)/transitionTime + 0.5;
var skyColor: Color = Color.Lerp(nightColor, dayColor, transition);
// apply skyColor to the appropriate object
}