Skip to content

Instantly share code, notes, and snippets.

@ExtevaXT
Created July 5, 2023 01:16
Show Gist options
  • Save ExtevaXT/08b7a3417097e5f6d1bfd00bad79dd9c to your computer and use it in GitHub Desktop.
Save ExtevaXT/08b7a3417097e5f6d1bfd00bad79dd9c to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class LightFlickering : MonoBehaviour {
public Light lightSource;
public float flickerInterval = 0.01f; // Time interval between flickers in seconds
public float flickerDuration = 3f; // Duration of each flicker in seconds
void OnEnable()
{
StartCoroutine(Flicker());
}
void Start()
{
StartCoroutine(Flicker());
}
IEnumerator Flicker()
{
while (true)
{
// Enable the light
lightSource.enabled = true;
// Wait for a random duration within the flicker duration
yield return new WaitForSeconds(Random.Range(0, flickerDuration));
// Disable the light
lightSource.enabled = false;
// Wait for a fixed flicker interval
yield return new WaitForSeconds(flickerInterval);
if (!enabled) break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment