Lerping over degrees

Ok, Im back again

I would need some calculation help here again.

I would like to Mathf.Lerp between -18 to +48 over 60 degrees. “-18 to +48 as sun moving exacly 60 degrees over sky”

If 1 was 60 then it would be easy to calculate but it is not 1, sooo hmm

Anyone know how I do that?

Are you saying that your input is a number between 0 and 60 degrees, and you need an output that is -18 to +48? If so, just subtract 18, and you’ll have your desired range. Because the size of the two ranges are equal, you only have to shift the input through addition/subtraction to transform it into the output range.

``````float t = 1f;
float d = Mathf.Lerp(-18, 48, t);
``````

d should be 48

I am making the temperature in the game go from -18 degrees Celsius to +48 degrees Celsius.

-18 is the temperature in the night time and +48 is the daytime temperature.
-18 and +48 is not set in stone and can be changed to whatever I would like.

So, when the sun is at 0 degrees of 360 degrees, it is in top center of sky shining down.
When sun is at 90 degrees, nighttime starts.
When sun is at 270 degrees, daytime is starts.
I want the temperature to start go up when the sun get to 270 degrees.
The temperature will go from -18 degrees Celsius “or whatever I choose” to +48 degrees Celsius “or whatever I choose” .
The span of wich the temperature will change is 60 degrees “from 270 degrees to 330 degrees”.

Using 1 as t would only set d to 48 without the steps in between -18 to 48.

I see now. The first thing you’ll need to do then is adjust your sun angle to a range from 0 to 1. 0 means minimum temperature, 1 means maximum temperature. You do this by subtracting the minimum angle (270), then dividing by the range between minimum angle and maximum angle (60). Once you have a value t between 0 and 1, you can use Mathf.Lerp(minTemperature, maxTemperature, t).

You can do a similar thing for the 90 to 150 degree range, for nightfall. And since Mathf.Lerp() automatically clamps the lerp parameter to between 0 and 1, you can simplify things a bit and still not worry about temperatures getting outside your desired range.

``````if (sunAngle < 180) // afternoon/night
{
temperature = Mathf.Lerp(minTemperature, maxTemperature, (sunAngle - 90f) / 60f);
}
else // morning
{
temperature = Mathf.Lerp(minTemperature, maxTemperature, (sunAngle - 270f) / 60f);
}
``````

EDIT: It helps to say “dividing” instead of “multiplying” when I actually mean division, because those are two very different operations!

1 Like

This. Normalising the output and input of systems is really useful if you are going to be messing around with paramatars mid development.

2 Likes

Thanks , works great now

It’s easy, and common, to get all confused about syntax and lerps and quaternion.euler. This is an 8th grade algebra problem. A trick to writing programs is to break it down into simple parts.

If you know anyone maybe 14-16 years old ask them how to solve an equation for the line through x=0,y=-18 and x=1,y=48. It sounds silly, but seeing a kid solve it helps you remember that you really did learn that stuff, and just forgot it. It makes it easier to pick out solvable math later on.

1 Like