Change Camera bg color as time progresses

Hi,

I’m trying to change my camera bg color from light blue to black.
I’ve had a few tries and it doesnt seem to work for some reason.
the part where “as time goes by do X” works great i added logs to see its working, but the actual changing of the color isnt working.

i’ve tried using HSV, i dont know what i need to do exactly to take it down slowly (i want it to be gradually turning into full black, not all at once), since i’m not changing between two numbers completely, i just need to take it down with a small amount each time, and its more than 1 color, its not only blue or green, its both.

this is how i want to take it down:

Hope its clear enough, and i’m apologize if its a dumb question, i’ve searched a lot and didnt find an answer or a thread for this.

Thanks in advance,

Are we talking about in a script? Are you trying to interpolate the color over time?

Yes exactly, in a script over time.
a little background - i’m creating a basic jetpack game and i want the background to become more and more dark until it gets to full black (like u’re in space).
so i’m starting with that blue-ish color, and i want to make my way down.

tried something out and it works, but although i’m using Lerp its still not smoothing out the way i want it to, it seems very clear that its changing , not really gradually.
thinking about making it “change every x seconds” maybe that will smooth things up?
i just want it to smoothly turn into black given a certain amount of time.

this is what i’ve got:

tempBackgroundColor = Camera.main.backgroundColor;
tempBackgroundColor.g = tempBackgroundColor.g - 0.05f;
tempBackgroundColor.b = tempBackgroundColor.b - 0.1f;
//Camera.main.backgroundColor = tempBackgroundColor;
Camera.main.backgroundColor = Color.Lerp(Camera.main.backgroundColor, tempBackgroundColor, 0.1f);

Pick your two colors and use Color.Lerp. This link has a good example usage if you look at the accepted answer:

https://stackoverflow.com/questions/51026918/unity-i-cant-seem-to-lerp-my-gameobjects-color

The first two parameters to Lerp are the “start” and “end” value, not the “current” and “target” value. You’re probably thinking more along the lines of MoveTowards, but Lerp doesn’t operate the same way.

If you want to make the background color get darker depending on your height, you could do something along the lines of this:

var t = Mathf.InverseLerp( startingHeight, endingHeight, currentHeight );
Camera.main.backgroundColor = Color.Lerp(startColor, endColor, t );

Where startColor is your blue, endColor is your black, startingHeight is the height at which you want to start fading to your endColor, endingHeight is the height at which you want to finish fading to your endColor, and currentHeight is the current player height.

1 Like

Thank you both for that information and explanation.
i now completely understand what Color.Lerp is!

I was able to use it by changing from Blue to Black, and using Time.deltaTime/duration to give a good time frame for lerping:

    void BackgroundColorChanger()
    {
        Camera.main.backgroundColor = Color.Lerp(Color.blue, Color.black, timer);

        if (timer < 1)
        {
            timer += Time.deltaTime / duration;
        }

    }
1 Like