Skip to content

Instantly share code, notes, and snippets.

@ted80
Created July 24, 2016 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ted80/276e03156ee85b52835b465954527645 to your computer and use it in GitHub Desktop.
Save ted80/276e03156ee85b52835b465954527645 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class LightAnimation : MonoBehaviour
{
public Light light;
[Range(0.1f, 5f)]
public float baseIntensity = 3.5f;
[Range(0.8f, 1f)]
public float repeat = 0.9f;
[Range(0.5f, 0.9f)]
public float repeatRequirement = 0.7f;
[Range(0.1f, 1f)]
public float minTimeout = 0.3f;
[Range(0.1f, 8f)]
public float randTimeout = 1f;
private float current = 1f;
private float target = 1f;
private float timeout = 0f;
void Start ()
{
}
void FixedUpdate ()
{
if (Random.value > 0.97f && current > 0.9f && timeout <= 0f)
{
target = Random.value * 0.7f + 0.3f;
current = 0.989f;
timeout = 1f;
}
if (current < 0.99f)
{
if(Mathf.Abs(current - target) < 0.05f)
{
target = 1f;
}
current = Mathf.Lerp(current, target, Time.deltaTime * 20f);
if(current >= 0.99f)
{
timeout = minTimeout + (Random.value * randTimeout);
}
else if(current > repeatRequirement && Random.value > repeat)
{
target = Random.value * 0.7f + 0.3f;
}
}
if(timeout > 0f)
{
timeout -= Time.deltaTime;
}
light.intensity = current * baseIntensity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment