Last active
February 8, 2019 10:45
-
-
Save AaronTrotter/af47cb6f4d4b6b1e94619934dc383c3f to your computer and use it in GitHub Desktop.
Unity Simple Light Flicker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Aaron Trotter aarontrotter.co.uk 2019 | |
// Free to use and modify. | |
// Do give credit where credit due. | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class SimpleLightFlicker : MonoBehaviour | |
{ | |
public float flickerContinuousSpeed = 1f; | |
private Light lightComponent = null; | |
public float flickerContinuousMaxIntensity = 0.4f; | |
public float flickerContinuousMinIntensity = 0.2f; | |
public bool fadeOut = false; | |
private bool isFlickering = false; | |
private bool isFadingOut = false; | |
private bool isFadingIn = false; | |
private bool doOnce = false; | |
private void OnEnable() | |
{ | |
if (!doOnce) | |
{ | |
lightComponent = GetComponent<Light>(); | |
doOnce = true; | |
} | |
lightComponent.intensity = flickerContinuousMaxIntensity; | |
} | |
public void Update() | |
{ | |
if (fadeOut) | |
{ | |
if (!isFadingOut) | |
{ | |
if (lightComponent.intensity != 0) | |
{ | |
StopAllCoroutines(); | |
isFadingOut = true; | |
isFadingIn = false; | |
isFlickering = false; | |
StartCoroutine(FadeLight(lightComponent, lightComponent.intensity, 0, 1f)); | |
} | |
} | |
} | |
else | |
{ | |
if (lightComponent.intensity >= 0 && lightComponent.intensity < flickerContinuousMinIntensity) | |
{ | |
if (!isFadingIn) | |
{ | |
StopAllCoroutines(); | |
isFadingOut = false; | |
isFadingIn = true; | |
isFlickering = false; | |
StartCoroutine(FadeLight(lightComponent, lightComponent.intensity, flickerContinuousMaxIntensity, 1f)); | |
} | |
} | |
else | |
{ | |
if (!isFlickering) | |
{ | |
StopAllCoroutines(); | |
isFadingOut = false; | |
isFadingIn = false; | |
isFlickering = true; | |
if (lightComponent.intensity == flickerContinuousMaxIntensity) | |
{ | |
StartCoroutine(FadeLight(lightComponent, lightComponent.intensity, flickerContinuousMinIntensity, flickerContinuousSpeed)); | |
} | |
else | |
{ | |
StartCoroutine(FadeLight(lightComponent, lightComponent.intensity, flickerContinuousMaxIntensity, flickerContinuousSpeed)); | |
} | |
} | |
} | |
} | |
} | |
public IEnumerator FadeLight(Light light, float from, float to, float time) | |
{ | |
float startTime = Time.time; | |
float timeElapsed = 0; | |
float journeyLength = from > to ? from - to : to - from; | |
float fracJourney; | |
float step = 0.0f; | |
if (from != to) | |
{ | |
for (float i = 0.0f; i < time; i = fracJourney) | |
{ | |
timeElapsed = (Time.time - startTime); | |
fracJourney = (timeElapsed / journeyLength); | |
step = fracJourney / time; | |
float val = Mathf.Lerp(from, to, step); | |
light.intensity = val; | |
yield return null; | |
} | |
} | |
yield return null; | |
light.intensity = to; | |
if (isFlickering) | |
{ | |
if (to == flickerContinuousMaxIntensity) | |
{ | |
StartCoroutine(FadeLight(lightComponent, lightComponent.intensity, flickerContinuousMinIntensity, flickerContinuousSpeed)); | |
} | |
else | |
{ | |
StartCoroutine(FadeLight(lightComponent, lightComponent.intensity, flickerContinuousMaxIntensity, flickerContinuousSpeed)); | |
} | |
} | |
else if (isFadingOut) | |
{ | |
isFadingOut = false; | |
} | |
yield return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment