- Home /
Blend into next color instead of jumping to next color
I have the following script segment in my day and night cycle I have been working on but instead it just jumping form SunRiseColor to normalColor I want it to blend to that color as it goes or change shades till it gets there. The _timeOfDay is my day code that its basing when do do each at. Right now I am doing sunRiseColor and sunRise2Color because I could not get a blend.
private void AdjustColor()
{
for(int cnt = 0; cnt < _sunScript.Length; cnt++)
{
if(_timeOfDay >20 && _timeOfDay < 38)
{
_sunScript[cnt].GetComponent<Light>().color = sunRiseColor;
_sunScript[cnt].GetComponent<LensFlare>().color = sunRiseColor;
}
if(_timeOfDay >38 && _timeOfDay < 40)
{
_sunScript[cnt].GetComponent<Light>().color = sunRise2Color;
_sunScript[cnt].GetComponent<LensFlare>().color = sunRise2Color;
}
if(_timeOfDay > 75 && _timeOfDay < 85)
{
_sunScript[cnt].GetComponent<Light>().color = sunSetColor;
_sunScript[cnt].GetComponent<LensFlare>().color = sunSetColor;
}
if(_timeOfDay > 85 && _timeOfDay < 100)
{
_sunScript[cnt].GetComponent<Light>().color = sunSet2Color;
_sunScript[cnt].GetComponent<LensFlare>().color = sunSet2Color;
}
if(_timeOfDay > 40 && _timeOfDay < 75)
{
_sunScript[cnt].GetComponent<Light>().color = normalColor;
_sunScript[cnt].GetComponent<LensFlare>().color = normalColor;
}
}
}
Any one please help me with this I Like how its come out but I cant figure out how to blend to the next color.
I would recomend to use iTween , although it depends what exactly you are doind.Anyway check this link ,there are samples that could help you. http://pixelplacement.com/itweenARCHIVE/
Answer by TrickyHandz · Oct 05, 2013 at 08:59 PM
Color.Lerp would do this for you beautifully. Here is an example of one case:
Light targetLight = sunScript[cnt].GetComponent<Light>();
LensFlare targetLensFlare = _sunScript[cnt].GetComponent<LensFlare>();
targetLight.color = Color.Lerp(targetLight.color, sunRiseColor,
Time.deltaTime * 0.5f);
targetLensFlare.color = Color.Lerp(targetLensFlare.color, sunRiseColor,
Time.deltaTime * 0.5f);
Let me know how that works out for you.
EDIT: Here is a link to the docs page for this - Color.Lerp
Ya, that has the effect I want, I was actually just playing with that myself the only problem I am hitting is its not changing fast enough.
You can change the dampening multiplier at the end to make it go faster. Just change work your way towards the speed you want:
targetLight.color = Color.Lerp(targetLight.color, sunRiseColor
Time.deltaTime * 5.0f);
That would make it go 10 times faster than my initial example.
Ok I see what I was doing wrong I was going down to try to go faster its up to go faster down to go slower. Ops lol thanks so much.
Not a problem. When you have a chance, just mark the question as answered so other people that are trying to do this can easily find a solution. Happy coding!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problem with color c# 1 Answer
Referencing color change when object is 'deselected' 2 Answers
Distribute terrain in zones 3 Answers
Multiple Button Modifications 2 Answers