- Home /
Smooth Camera Background Color Changing?
I am trying to smoothly change the camera's background color between two colors that are picked randomly. I've already achieved that, but then I started noticing that there is a flash whenever a new color is picked. I've uploaded a video of the problem on this link. And this is the script that I'm currently using:
 public Color color1;
 public Color color2;
 
 float time;
 float time2;
 float transition;
 
 int firstColor = 0;
 void Update()
 {
     if (firstColor == 0)
     {
         color1 = Random.ColorHSV(Random.value, Random.value);
         color2 = Random.ColorHSV(Random.value, Random.value);
 
         firstColor = 1;
     }
 
     Camera.main.backgroundColor = Color.Lerp(color2, color1, transition);
 
     time += Time.deltaTime;
     time2 += Time.deltaTime;
     transition = time2 / 5;
 
     if (time > 5)
     {
         color2 = color1;
         color1 = Random.ColorHSV(Random.value, Random.value);
 
         time = 0;
         time2 = 0;
     }
 }
Any help is very much appreciated.
Answer by pako · Nov 02, 2017 at 08:05 PM
I think what's happening here is that after you set color1 in line 30 as a new random color, transition has the value of 1 (clamped).
So, before transition is set in the next frame at line 25, line 21 executes, and since transition = 1, the new color1 is returned, hence the "flashing" of the new color1.
I think that if you reset transition = 0, after you reset time and time2 to 0 (line 32, 33), should fix this.
Never crossed my $$anonymous$$d that the issue would be there. Thank you for solving another problem of $$anonymous$$e.
Your answer
 
 
             Follow this Question
Related Questions
GameObject keeps acting like gameObject. 1 Answer
When a ball is clicked and background color matches it's color I get a point? 0 Answers
Instantiating multiple sprites and assigning different colors for each 1 Answer
Access properties of Material through script. 3 Answers
Make the cube have the same color as the background every 3 seconds 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                