Sun intensity with day/night cycle (struggling with light intensity fading at night)

Hi all,

I’m working on a day/night cycle script after watching some Youtube tutorials and reading a little bit. All is working well so far, you can see from my code snippet below, there’s a time of day which goes from 0.0f to 1.0f. Up to middday, the sun increases in brightness from 0 to 1 nicely using Mathf.Lerp.

However I’m struggling to get my head around how I get the intensity to go the other way (decrease) once the midpoint 0.5f has been passed.

        // If it is morning..
        if (currentTimeOfDay < 0.5)
        {
            sun.intensity = Mathf.Lerp(0, 1, currentTimeOfDay * 2);
        }

        // If it is afternoon/evening...
        if (currentTimeOfDay >= 0.5)
        {
            sun.intensity = Mathf.Lerp(1, 0, currentTimeOfDay);
        }

With my code above, the sun intensity “snaps” from 1 at midday to 0.5 immediately (due to how the Mathf.Lerp is working there obviously).

How can I have the intensity lerp from 1 to 0 based on the time of day 0.5 → 1.0 range?

Any thoughts?

Thanks in advance everyone.

It’s related to why you needed to multiply currentTimeOfDay by 2 in your morning script.

Think about what kind of formula would give you the results you want. Your starting point is 0.5 and ending is 1.0. You need a formula that changes 0.5 to 0.0 and 1.0 to 1.0.

I think this will work

Think about this in terms of shifts and scaling. compared to the morning, you need to “shift” backwards half a day. Then, you need the same “scaling” to turn half a day into a whole 1.0.
(currentTimeOfDay - 0.5) * 2

1 Like

You eisenpony, are a genius.

Thank you so much.

(maths isn’t my strong suit but looking at your code it’s really simple and I’m ashamed I’m didn’t think of it) :smile: