Skip to content

Instantly share code, notes, and snippets.

@rocksockm
Created June 14, 2021 21:52
Show Gist options
  • Save rocksockm/81ec3bf2b601dbaf8402c749628af2cd to your computer and use it in GitHub Desktop.
Save rocksockm/81ec3bf2b601dbaf8402c749628af2cd to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QuakeLightFlicker : MonoBehaviour
{
public float maxLightIntensity = 2f;
public string flickerString = "mmamammmmammamamaaamammma";
public Light light;
int index = 0;
float defaultInt = 1;
public GameObject optionalToggleObject;
char[] chars;
// Start is called before the first frame update
void Start()
{
light = GetComponent<Light>();
defaultInt = light.intensity;
StartCoroutine(Flicker());
chars = flickerString.ToCharArray();
}
IEnumerator Flicker()
{
while (true)
{
yield return new WaitForSeconds(0.1f);
if (light)
{
int charIndex = (int)chars[index] % 32;
float value = ((float)charIndex / 32f) * maxLightIntensity;
light.intensity = value;
if (flickerString.Substring(index, 1) == "a")
{
if (optionalToggleObject)
optionalToggleObject.SetActive(false);
}
else
{
//light.intensity = 0.1f;
if (optionalToggleObject)
optionalToggleObject.SetActive(true);
}
index = (index + 1) % flickerString.Length;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment