- Home /
Increase a variable from a value to another value in a range of time
Hello guys, I'm here to ask you an advice because I don't have any idea D: . Sorry for the title, it's not easy to describe this problem in a simple sentence. So I'm making a Night Day Cycle (I followed the BurzBurg Arcade Tutorial, but I'm making my own now inspired by Burz) and I have a problem with the Sunset and Sunrise.
My goal is to decrease/increase the light of the sun pretty well (when it's midday, the Intensity of the sun must be 1, when the sun sets, the intensity must be 0).
I split the day in 5 moments: Sunrise, Midday, Noon, Sunset and MidNight and every moments begin at a specific "hour" (for example: a day is 60 seconds long, so midnight will be 0, midday 30 (DayDuration in Seconds / 2), etc...
I have a variable called ActualTime that increase at every frame and it's porpouse is to keep up the time like a clock.
So the morning will be long (Midday - Dawn). My problem is that I can't figure out the operation to make sure I have a correct increase value for the sun's light in a way that, when is Midday, the intensity will be 1, and even if I change the Duration of the day, the sun will always have 1 of intensity when is Midday.
This is part of the code, the increaseRate is wrong
IncreaseRate = (Midday - dawn) /1000f;
if((_ActualTime > Dawn) && (_TempoGiornaliero < Midday)) {
if (Suns[0].light.intensity <= 1.0f){
Suns[0].light.intensity += IncreaseRate * Time.deltaTime;
}
}
I want to make something like this:
When the Dawn begin, the sun's intensity begin to increase so when it's Midday, the intensity will be 1, but in that moment, not before, not after.
My scripts can work with "custom value" that I can put in the inspector... but I never liked this kind of solution :P. I know that the solution will be pretty simple, but right now I don't have any ideas D:.
Sorry for the bad explanation. Have a nice day!
Answer by Baste · Sep 05, 2014 at 11:02 AM
There's a great trick you can do here with animation curves.
First, attach a public animation curve to your script:
public AnimationCurve lightCurve;
You'll use this to set the light of the "sun" at different points of the day. Go to the inspector, and click the lightCurve part of your script, and you'll get an editor. Set the values of the curve by double clicking to make points, and dragging them around.
You say you want midday intensity to be 1, and 60 second day cycles. Then you should make a node at 30 on the time (x) axis and 1 on the value (y) axis, with the 0 and 60 points having a value at or close to 0:
Then, in your script, you set the light intensity to the value of the curve at the current time. In your code, this would be:
Suns[0].light.intensity = lightCurve.Evaluate(Time.time % 600000)
% 600000 makes the evaluation wrap every 60000 milliseconds, or 60 seconds. So when you get to 61 seconds, the curve is evaluated at 1 seconds.
I hope that helps! In general, animation curves can be used for an amazing range of things to jumping and knockback patterns to whatever. The Animation part of the name isn't something you should care too much about.
Thank you! I will use this trick for the Sun Light and for the Ambient Light too!
Great!
It'd be nice if you could mark the answer as accepted, so it disappears from the list of unanswered questions. :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613162306im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to change a value over time in a coroutine ? 2 Answers
Make value change to anoter over time and back loop 3 Answers
Reducing a value over time 2 Answers
Realtime daylight cycle? 1 Answer
UI Slider Value Increases Far Too Slowly With Controller. 1 Answer