Advertisement
ninjapretzel

Flicker

May 11th, 2014
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. //Coded by ninjapretzel
  6. //Flicker behaviour
  7. //Add to a GameObject to make its renderer 'flicker'
  8. public class Flicker : MonoBehaviour {
  9.    
  10.     public bool animate = true;
  11.    
  12.     public float tickTime = .1f;    //Time in seconds per 'tick' (.1 = .1sec full alpha, .1sec reduced alpha, .1sec full, etc)
  13.     public float alphaScale = .3f;  //How transparent are the 'faded' ticks?
  14.    
  15.     float timeout = 0;              //timer to keep track of current time this tick.
  16.     float fullAlpha = 1;            //Keep track of full alpha. We will grab this info from the material on start.
  17.     bool full = true;               //Flag to keep track of if we should use the full or reduced alpha
  18.    
  19.     void Start() {
  20.         fullAlpha = renderer.material.color.a;
  21.         timeout = 0;
  22.         full = true;
  23.     }
  24.    
  25.     void Update() {
  26.         Color c = renderer.material.color;
  27.         c.a = fullAlpha;
  28.        
  29.         //Is the effect animating?
  30.         if (animate) {
  31.             //Accumulate time into the timer
  32.             timeout += Time.deltaTime;
  33.            
  34.             //Process all ticks that would have happened over the deltaTime
  35.             //(incase of a delay, we don't want it to instantly flip for a few frames)
  36.             while (timeout > tickTime) {
  37.                 timeout -= tickTime;
  38.                 full = !full;
  39.                 //Subtract the time for that tick, and flip the fade flag.
  40.             }
  41.            
  42.             //If we are not full this frame, set the reduced alpha value.
  43.             if (!full) {
  44.                 c.a *= alphaScale;
  45.             }
  46.            
  47.         }
  48.        
  49.         //This will apply the full alpha if we are not animating,
  50.         //And then partial alpha half the time if we are.
  51.         renderer.material.color = c;
  52.     }
  53.    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement