Hi. I’ve got a flickering light set up, and I’ve been wondering if there’s a way to have a sound attached to it flicker along with the light. I know I could just make the sound flicker “hardcoded” in the actual audio, but I kinda want the flickering to be accurate. Here’s my code for the light:
using UnityEngine;
using System.Collections;
public class flick : MonoBehaviour {
private int randomizer = 0;
private float brightness;
public int freq = 20;
public int lowfreq = 1;
public AudioClip lightSound;
private AudioSource lightAudio;
private Light lightObject;
public bool active;
void Awake ()
{
lightAudio = GetComponent<AudioSource> ();
lightAudio.clip = lightSound;
lightObject = GetComponent<Light> ();
brightness = lightObject.intensity;
}
public void OnActive()
{
active = !active;
lightObject.enabled = !lightObject.enabled;
if (active) {
lightAudio.Play ();
} else
lightAudio.Stop ();
}
void Update() {
if (active) {
if (randomizer <= lowfreq) {
lightObject.intensity = 0;
} else
lightObject.intensity = brightness;
randomizer = Random.Range (0, freq);
}
}
}
So when the light is active, I have it so a looping fluorescent light sound plays. I’ve tried adding something like “lightAudio.volume = 0;” and “lightAudio.volume = 1;” to the update section, but it doesn’t appear to work. Unless it’s flickering too fast and my ears just aren’t picking it up.
I’ve also tried a .Pause and .Play, on lightAudio but, while it works, it is not synced to the light flicker.
Is it even worth attempting to fix, as it’s just a minor detail?