Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Flickering Light

Discussion in 'Editor & General Support' started by rottenvicious, May 15, 2007.

Thread Status:
Not open for further replies.
  1. rottenvicious

    rottenvicious

    Joined:
    Nov 13, 2006
    Posts:
    13
    Does anyone have an idea about how to apply a flicker effect to a light?
    Any help would be much appreciated
    Cheers
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Here try this:

    Code (csharp):
    1. var flickerSpeed : float = 0.1;
    2.  
    3. while (true) {
    4.     light.enabled = true;
    5.     yield WaitForSeconds (flickerSpeed);
    6.     light.enabled = false;
    7.     yield WaitForSeconds (flickerSpeed);
    8. }
     
    zoshida likes this.
  3. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    This would probably be more realistic
    Code (csharp):
    1.  
    2. var minFlickerSpeed : float = 0.1;
    3. var maxFlickerSpeed : float = 1.0;
    4.  
    5. function Update()
    6. {
    7.      light.enabled = true;
    8.      yield WaitForSeconds (Random.Range(minFlickerSpeed, maxFlickerSpeed );
    9.      light.enabled = false;
    10.      yield WaitForSeconds (Random.Range(minFlickerSpeed, maxFlickerSpeed );
    11. }
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    This one should work

    Code (csharp):
    1. var flickerSpeed : float = 0.07;
    2. private var randomizer : int = 0;
    3. while (true) {
    4.     if (randomizer == 0) {
    5.         light.enabled = false;
    6.     }
    7.     else light.enabled = true;
    8.     randomizer = Random.Range (0, 1.1);
    9.     yield WaitForSeconds (flickerSpeed);
    10. }
     
  5. rottenvicious

    rottenvicious

    Joined:
    Nov 13, 2006
    Posts:
    13
    thanks guys
    im a little intrigued to know why you guys are posting on the forum at 3 in the morning though!?!
    i have a valid excuse, i have a deadline to meet tomorrow!

    cheers
     
  6. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I'm in California, where it's about 7:30 PM. Where are you?
     
  7. rottenvicious

    rottenvicious

    Joined:
    Nov 13, 2006
    Posts:
    13
    ahhh i see that would make sense!

    im in good old england, stuck in a studio at my university, totally shattered

    cheers for your help though, its very much appreciated
     
  8. Harry1960

    Harry1960

    Joined:
    May 15, 2007
    Posts:
    136
    You could use the following script to animate your light in various time based ways. If you use "noise" the flickering will be randomized. All other functions will increse and decrease the light color with the specified parameters.

    Code (csharp):
    1. // Properties
    2. var waveFunction : String = "sin"; // possible values: sin, tri(angle), sqr(square), saw(tooth), inv(verted sawtooth), noise (random)
    3. var base : float = 0.0; // start
    4. var amplitude : float = 1.0; // amplitude of the wave
    5. var phase : float = 0.0; // start point inside on wave cycle
    6. var frequency : float = 0.5; // cycle frequency per second
    7.  
    8. // Keep a copy of the original color
    9. private var originalColor : Color;
    10.  
    11. // Store the original color
    12. function Start () {
    13.     originalColor = GetComponent(Light).color;
    14. }
    15.  
    16. function Update () {
    17.   var light : Light = GetComponent(Light);
    18.   light.color = originalColor * (EvalWave());
    19. }
    20.  
    21. function EvalWave () {
    22.   var x : float = (Time.time + phase)*frequency;
    23.   var y : float;
    24.  
    25.   x = x - Mathf.Floor(x); // normalized value (0..1)
    26.  
    27.   if (waveFunction=="sin") {
    28.     y = Mathf.Sin(x*2*Mathf.PI);
    29.   }
    30.   else if (waveFunction=="tri") {
    31.     if (x < 0.5)
    32.       y = 4.0 * x - 1.0;
    33.     else
    34.       y = -4.0 * x + 3.0;  
    35.   }    
    36.   else if (waveFunction=="sqr") {
    37.     if (x < 0.5)
    38.       y = 1.0;
    39.     else
    40.       y = -1.0;  
    41.   }    
    42.   else if (waveFunction=="saw") {
    43.       y = x;
    44.   }    
    45.   else if (waveFunction=="inv") {
    46.     y = 1.0 - x;
    47.   }    
    48.   else if (waveFunction=="noise") {
    49.     y = 1 - (Random.value*2);
    50.   }
    51.   else {
    52.     y = 1.0;
    53.   }        
    54.   return (y*amplitude)+base;     
    55. }
    56.  
    [/code]
     
  9. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Harry, in your script what part of the script changes the brightness of the light?

    thanks
     
  10. Harry1960

    Harry1960

    Joined:
    May 15, 2007
    Posts:
    136
    Daniel the light color computation is done in the update function. Since color is a vector (rgba) you can use scalar multiplication to fade from original color (scalar value=1.0) to black (scalar value is 0.0).
    This is the reason why we have to store the original color value (which is done in the Start function).
    EvalWave() delivers the amplitude (plus the base value) of the given wave at the current point of time.
    Just put the code into a script and attach it to a light source and you'll see it work.

    Code (csharp):
    1. // light reference
    2. var light : Light = GetComponent(Light);
    3. // this part changes the light via vector multiplication
    4. light.color = originalColor * (EvalWave());
    5.  
     
  11. Harry1960

    Harry1960

    Joined:
    May 15, 2007
    Posts:
    136
    Daniel the light color computation is done in the update function. Since color is a vector (rgba) you can use scalar multiplication to fade from original color (scalar value=1.0) to black (scalar value is 0.0).
    This is the reason why we have to store the original color value (which is done in the Start function).
    EvalWave() delivers the amplitude (plus the base value) of the given wave at the current point of time.
    Just put the code into a script and attach it to a light source and you'll see it work.

    Code (csharp):
    1. // light reference
    2. var light : Light = GetComponent(Light);
    3. // this part changes the light via vector multiplication
    4. light.color = originalColor * (EvalWave());
    5.  
     
  12. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Okay Thanks. Okay Thanks. :D
     
  13. X5900

    X5900

    Joined:
    Jun 19, 2009
    Posts:
    11
    I have a strange problem when using the light flicker script and it appears only when played in fullscreen. In the editor or when played windowed it works great. In fullscreen the whole screen suddenly shows bold horizontal lines, looks like an overlay problem.

    Could someone try this out in fullscreen?
     
  14. Discord

    Discord

    Joined:
    Mar 19, 2009
    Posts:
    1,008
    Wow. Talk about an old thread. There are a number of scripts presented in this thread, which one did you try to use?
     
  15. X5900

    X5900

    Joined:
    Jun 19, 2009
    Posts:
    11
    This one:

    Code (csharp):
    1.  
    2. // Properties
    3. var waveFunction : String = "sin"; // possible values: sin, tri(angle), sqr(square), saw(tooth), inv(verted sawtooth), noise (random)
    4. var base : float = 0.0; // start
    5. var amplitude : float = 1.0; // amplitude of the wave
    6. var phase : float = 0.0; // start point inside on wave cycle
    7. var frequency : float = 0.5; // cycle frequency per second
    8.  
    9. // Keep a copy of the original color
    10. private var originalColor : Color;
    11.  
    12. // Store the original color
    13. function Start () {
    14.    originalColor = GetComponent(Light).color;
    15. }
    16.  
    17. function Update () {
    18.   var light : Light = GetComponent(Light);
    19.   light.color = originalColor * (EvalWave());
    20. }
    21.  
    22. function EvalWave () {
    23.   var x : float = (Time.time + phase)*frequency;
    24.   var y : float;
    25.  
    26.   x = x - Mathf.Floor(x); // normalized value (0..1)
    27.  
    28.   if (waveFunction=="sin") {
    29.      y = Mathf.Sin(x*2*Mathf.PI);
    30.   }
    31.   else if (waveFunction=="tri") {
    32.      if (x < 0.5)
    33.        y = 4.0 * x - 1.0;
    34.      else
    35.        y = -4.0 * x + 3.0;  
    36.   }      
    37.   else if (waveFunction=="sqr") {
    38.      if (x < 0.5)
    39.        y = 1.0;
    40.      else
    41.        y = -1.0;  
    42.   }      
    43.   else if (waveFunction=="saw") {
    44.        y = x;
    45.   }      
    46.   else if (waveFunction=="inv") {
    47.      y = 1.0 - x;
    48.   }      
    49.   else if (waveFunction=="noise") {
    50.      y = 1 - (Random.value*2);
    51.   }
    52.   else {
    53.      y = 1.0;
    54.   }          
    55.   return (y*amplitude)+base;    
    56. }
    When I force V-Synch in the project settings, it works fine, even in fullscreen.
     
  16. Harry1960

    Harry1960

    Joined:
    May 15, 2007
    Posts:
    136
    The script shouldn't cause the effect but try to use LateUpdate() instead of Update(). If this doesn't help check your graphic card driver since you mention that v-sync prevents that problem.
     
  17. X5900

    X5900

    Joined:
    Jun 19, 2009
    Posts:
    11
    I tried LateUpdate, but still the same effect. I even tested it on 4 different systems, Nvidia and ATI based. This is so strange, it appears always only on Fullscreen and without forcing V-Synch.

    What's funny is that I've used a different script that goes for the same flicker effect but takes a slightly different approach and the result was the same error. I kinda looks like tearing but all over the place.
     
  18. Harry1960

    Harry1960

    Joined:
    May 15, 2007
    Posts:
    136
    Except the vsync solution - which normally should be turned on by default because it doesn't make sense to render more frames than your monitor can display and cause these artifacts by showing 2 rendered images during the same screen refresh - you should check whether the are any z-buffer conflicts.
    Also check the flicker intervall wich should not be smaller than the monitor frequency (1000ms/60Hz = 16.7ms)
     
  19. Harry1960

    Harry1960

    Joined:
    May 15, 2007
    Posts:
    136
    Except the vsync solution - which normally should be turned on by default because it doesn't make sense to render more frames than your monitor can display and cause these artifacts by showing 2 rendered images during the same screen refresh - you should check whether the are any z-buffer conflicts.
    Also check the flicker intervall wich should not be smaller than the monitor frequency (1000ms/60Hz = 16.7ms)
     
  20. chronopsis

    chronopsis

    Joined:
    May 1, 2006
    Posts:
    25
    a slight mod to the above:

    Code (csharp):
    1. var minFlickerSpeed : float = 0.01;
    2. var maxFlickerSpeed : float = 0.1;
    3. var minLightIntensity : float = 0;
    4. var maxLightIntensity : float = 1;
    5.  
    6. while (true)
    7. {
    8.      light.enabled = true;
    9.      light.intensity = Random.Range(minLightIntensity, maxLightIntensity);
    10.      yield WaitForSeconds (Random.Range(minFlickerSpeed, maxFlickerSpeed ));
    11.      light.enabled = false;
    12.      yield WaitForSeconds (Random.Range(minFlickerSpeed, maxFlickerSpeed ));
    13. }
     
  21. Filax

    Filax

    Guest

    Joined:
    Mar 30, 2011
    Posts:
    8
    Another version:

     
  22. dashmasterful

    dashmasterful

    Joined:
    Feb 3, 2010
    Posts:
    166
    for once can someone write something is c#
     
  23. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    Unless you need a specific effect, such as flickering, flickering then permanently on, if you just want the best flickering light for the least effort, use a phase shifted sine on the intensity and colour. Looks beautiful, works for almost everything from burning torches to flashlights to overhead strip lights.
     
  24. ViralVector

    ViralVector

    Joined:
    Aug 20, 2011
    Posts:
    23
    using UnityEngine;
    using System.Collections;

    public class ViralLight : MonoBehaviour
    {
    public enum lightType {flicker,pulsate}
    public lightType type = lightType.flicker;

    public Light light;
    public float speed;
    public float noise;
    // Use this for initialization
    void Start (){
    light.enabled = false; //Initially turn off Light
    if(type == lightType.flicker){
    StartCoroutine(Flicker());
    }else if(type == lightType.pulsate){

    }

    }

    // Update is called once per frame
    void Update ()
    {

    }
    IEnumerator Flicker (){
    light.enabled = true;
    float randNoise = Random.Range(-1,1)* Random.Range(-noise,noise);
    yield return new WaitForSeconds(speed+randNoise);
    light.enabled = false;
    yield return new WaitForSeconds(speed);
    StartCoroutine(Flicker());
    }

    }

    Simple Shell for a C# Light Script/////UNTESTED!!!
    MORE FREE ASSETS FOUND ON MY WEB HERE
     
    Last edited: Jan 15, 2012
    Lylkai likes this.
  25. ejones

    ejones

    Joined:
    Sep 27, 2012
    Posts:
    1
    I have created a modified version in C#. It allows to select the color channel and you can also choose if you want to affect the intensity, too.

    You can add this script multiple times to an object with different color channels and different phases to get interesting results. The intensity should only be affected by one script.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public enum enColorchannels
    6. {
    7.     all =0,
    8.     red =1,
    9.     blue =2,
    10.     green =3
    11. }
    12. public enum enWaveFunctions
    13. {
    14.     sinus =0,
    15.     triangle =1,
    16.     square =2,
    17.     sawtooth =3,
    18.     inverted_saw =4,
    19.     noise =5
    20. }
    21. public class LightColorAnimation : MonoBehaviour {
    22.    
    23.     public enColorchannels colorChannel = enColorchannels.all;
    24.     public enWaveFunctions waveFunction= enWaveFunctions.sinus;
    25.     public float offset =0.0f; // constant offset
    26.     public float amplitude = 1.0f; // amplitude of the wave
    27.     public float phase = 0.0f; // start point inside on wave cycle
    28.     public float frequency = 0.5f; // cycle frequency per second
    29.     public bool affectsIntensity = true;
    30.    
    31.     // Keep a copy of the original values
    32.     private Color originalColor;
    33.     private float originalIntensity;
    34.  
    35.    
    36.     // Use this for initialization
    37.     void Start () {
    38.         originalColor = GetComponent<Light>().color;
    39.         originalIntensity = GetComponent<Light>().intensity;
    40.     }
    41.    
    42.     // Update is called once per frame
    43.     void Update () {
    44.         Light light = GetComponent<Light>();
    45.         if (affectsIntensity)
    46.             light.intensity = originalIntensity * EvalWave();
    47.        
    48.         Color o = originalColor;       
    49.         Color c = GetComponent<Light>().color;
    50.        
    51.         if (colorChannel == enColorchannels.all)
    52.             light.color = originalColor * EvalWave();
    53.         else
    54.         if (colorChannel == enColorchannels.red)   
    55.             light.color = new Color(o.r* EvalWave(),c.g,c.b ,c.a);
    56.         else
    57.         if (colorChannel == enColorchannels.green) 
    58.             light.color = new Color(c.r,o.g* EvalWave(),c.b ,c.a); 
    59.         else // blue       
    60.             light.color = new Color(c.r,c.g, o.b * EvalWave(),c.a);
    61.     }
    62.    
    63.     private float EvalWave()
    64.     {
    65.         float x = (Time.time + phase)*frequency;
    66.         float y;
    67.         x = x - Mathf.Floor(x); // normalized value (0..1)
    68.         if (waveFunction==enWaveFunctions.sinus)
    69.         {
    70.             y = Mathf.Sin(x*2f*Mathf.PI);
    71.         }
    72.         else if (waveFunction==enWaveFunctions.triangle) {
    73.             if (x < 0.5f)
    74.                 y = 4.0f * x - 1.0f;
    75.             else
    76.                 y = -4.0f * x + 3.0f;  
    77.         }    
    78.         else if (waveFunction==enWaveFunctions.square) {   
    79.             if (x < 0.5f)      
    80.               y = 1.0f;    
    81.             else       
    82.               y = -1.0f;       
    83.         }          
    84.         else if (waveFunction==enWaveFunctions.sawtooth) {     
    85.               y = x;       
    86.         }          
    87.         else if (waveFunction==enWaveFunctions.inverted_saw) {     
    88.             y = 1.0f - x;      
    89.         }          
    90.         else if (waveFunction==enWaveFunctions.noise) {    
    91.             y = 1f - (Random.value*2f);    
    92.         }      
    93.         else {     
    94.             y = 1.0f;
    95.        
    96.         }          
    97.         return (y*amplitude)+offset;    
    98.        
    99.     }
    100.    
    101. }
    102.  
    103.  
     
  26. Vis-Studios

    Vis-Studios

    Joined:
    Mar 29, 2013
    Posts:
    4
    Can someone please tell which scripting language this is using? JS, CS, Or Boo? Cause it seems to be bad syntax in all of them :3
     
  27. devfo

    devfo

    Joined:
    Jul 24, 2012
    Posts:
    49
    It should be Javascript, and yes I got an error too. The post is 6 years old now, so it must be outdated.
     
  28. Deleted User

    Deleted User

    Guest

    My turn to contribute to this 7-year-old thread! :)

    I recommend toying around with the halo size of your light as well, to make the firey light seem more alive. In the if-statement mentioned several times above, I put:
    var RandomHaloRange = Random.Range(0.35,0.45);
    RenderSettings.haloStrength=RandomHaloRange;
     
  29. Josh-Naylor

    Josh-Naylor

    Administrator

    Joined:
    Jul 1, 2014
    Posts:
    216
    exactly
     
Thread Status:
Not open for further replies.