color.lerp over in game time

i have a day and night cycle and i want to change colors over time. Is there a way to lerp from (check script) “iGameTime”? like, when the counter is 0 to 56 then change from orange to blue.

Dont mind the “Skycolor = new Vector4 (0.23f + (0.009f * iGameTime), 0.26f + (0.005f * iGameTime), 0.55f, 0f);” stuff :wink:

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 Color Skycolor;

	public bool dawn;
	public bool day;
	public bool dusk;
	public bool night;

	public Color colorStart = Color.red;
	public Color colorEnd = Color.green;
	

	// 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) % 255;
		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);
			Skycolor = Color.Lerp (colorStart, colorEnd, Time.time / 10f);
			
			
			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;
			
		}
		
	}
	

}

You could use a Gradient for this.

If you create a public Gradient variable you can use the Inspector to intuitively edit a multi-point gradient.

Then in your code just call gradient.Evaluate() to get the color for any given time.


Otherwise if using Color.Lerp() it would be a matter of calculating progress through a period expressed as a number between 0 and 1.

Here is an example that might handle the dusk period

// at the top of your class
public int duskStartTime = 112;
public int nightStartTime  = 169;
public Color duskStartColor = new Color32(137, 167, 209);
public Color nightStartColor = new Color32(16, 74, 150);

// down in the Update
else if (iGameTime > duskStartTime && iGameTime <= nightStartTime) {
    float progress = (iGameTime - duskStartTime) / (nightStartTime - duskStartTime);
    Color current = Color.Lerp(duskStartColor, nightStartColor, progress);

    dawn = false;
    day = false;
    dusk = true;
    night = false;
}