Fade camera background colour ... but not PingPong

Hi all,

I want to fade the camera’s background colour from one colour to another over, say, 10 seconds. I found this and it works but it pingpongs it … I simply need it to fade once and stop.

Can someone quickly mod this for me to show me how?

// animate background color to fade from color 1 to color 2
var color1 : Color;
var color2 : Color;
var duration = 10.0;

// Set clear flags to color -- not needed, camera already set to this
// camera.clearFlags = CameraClearFlags.SolidColor;

function Update () {
    var t : float = Mathf.PingPong (Time.time, duration) / duration;
    camera.backgroundColor = Color.Lerp (color1, color2, t);
}

I think I could do it by calling a coroutine in the function Awake () that runs once when the scene loads, correct? It’s the fact that it’s in Update () that also makes it continue to ping pong?

Thanks.

Probably the easiest way to do this is with Mathf.InverseLerp:-

function Update () {
    var t : float = Mathf.InverseLerp (0, duration, Time.time);
    camera.backgroundColor = Color.Lerp (color1, color2, t);
}

Yes, that’s it, thanks. I will also move it out of Update () so it runs once when the scene loads. Just for my own organization and understanding.