- Home /
ThunderClap
I am trying to follow a course to learn Unity, and I do exactly what appears in the video:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ThunderClap : MonoBehaviour { bool canFlicker = true; private Light light; public AudioClip clip;
void Awake()
{
light = GetComponent<Light>();
}
private void Update()
{
StartCoroutine(Flicker());
}
IEnumerator Flicker()
{
if (canFlicker)
{
canFlicker = false;
audio.PlayOneShot(clip);
light.enabled = true;
yield return new WaitForSeconds(Random.Range(0.1f,0.4f));
light.enabled = false;
yield return new WaitForSeconds(Random.Range(0.1f,5f));
canFlicker = true;
}
}
But the sounds is not playing at the same time of the lightning. Anyone to help me?
Its not going to change the outcome of the code, but it would also be better to wrap the Startcoroutine in the if(canflicker) statement rather than checking in the coroutine. Because otherwise you are starting a coroutine every frame which then does nothing.
if(canFlicker)
Startcoroutine(Flicker());
canFlicker = false;
}
private IEnumerator Flicker() {
// Do stuff
canFlicker = true;
}
Answer by BradyIrv · Jun 09, 2019 at 03:02 AM
Does the audio clip (the actual audio file of the thunder sound) play immediately when you listen to it normally? Taking unity out of the equation and only listening to the audio clip itself is there a delay before the Thunder?
I'm thinking that it could be the audio file itself that has a delay because the rest of your code looks fine. Assuming that you can hear it and it's just not the right timing.
Firstly, Thanks!
Unfortunately is not the audio. And no, I am not hearing anything.
About the script, something must be wrong because it is not working. the thing is that I am using this script, but whatever script that allows me to do a lightning at the same time of a thunderclap it will be good. So, maybe someone knows another formula to do it smartly.
If you're not hearing anything and there are no errors and you've debugged that the Coroutine ran.. then it must be an issue in the audio script. Can you post the code from that? On line 18 you reference the audio script.
Ok, now I fix the issue of the synchronization between lightning and thunder, but, how can I listen the thunder in every place? Because I am only hearing it when I am at some point in the level.
Thanks!!!