I guys i create a simple script that flashs a Directional Light over time that simulates a thunder and then i apply a sound to the flash. It works but not perfectly because 2 things:
- Low delay between thunders;
- The sound clid has 5s;
So what i would like to know is how can i adjust this for a big delay between flashes, and if its possible to play just 2s of the audio clip for example.
Im not good coder so if someone can help me i appreciate. This is my actual code:
using UnityEngine;
using System.Collections;
public class LightningFlash : MonoBehaviour
{
public float minTime = 0.5f;
public float threshhold = 0.5f;
public Light myLight;
public AudioClip thunder;
private float lastTime = 0.5f;
// Update is called once per frame
void Update ()
{
if ((Time.time - lastTime) > minTime)
{
if(Random.value > threshhold)
{
myLight.enabled = true;
PlaySound(0);
}
else
{
myLight.enabled = false;
lastTime = Time.time;
}
}
}
void PlaySound(int clip)
{
AudioSource audio = GetComponent<AudioSource>();
audio.clip = thunder;
audio.Play ();
}
}