private void CheckForLightning() { // time for another strike? if (Time.time >= script.nextLightningTime) { bool intense = UnityEngine.Random.value < script.LightningIntenseProbability; script.StartCoroutine(ProcessLightning(null, null, intense, script.LightningAlwaysVisible)); } }
public IEnumerator ProcessLightning(bool intense, bool visible) { float sleepTime; AudioClip[] sounds; float intensity; script.lightningInProgress = true; if (intense) { float percent = UnityEngine.Random.Range(0.0f, 1.0f); intensity = Mathf.Lerp(2.0f, 8.0f, percent); sleepTime = 5.0f / intensity; script.lightningDuration = UnityEngine.Random.Range(script.LightningShowTimeRange.x, script.LightningShowTimeRange.y); sounds = script.ThunderSoundsIntense; } else { float percent = UnityEngine.Random.Range(0.0f, 1.0f); intensity = Mathf.Lerp(0.0f, 2.0f, percent); sleepTime = 30.0f / intensity; script.lightningDuration = UnityEngine.Random.Range(script.LightningShowTimeRange.x, script.LightningShowTimeRange.y); sounds = script.ThunderSoundsNormal; } if (script.skyboxMaterial != null && script.ModifySkyboxExposure) { script.skyboxMaterial.SetFloat("_Exposure", Mathf.Max(intensity * 0.5f, script.skyboxExposureStorm)); } // determine number of lightning strikes int count; int num = UnityEngine.Random.Range(0, 100); if (num > 95) { count = 3; } else if (num > 80) { count = 2; } else { count = 1; } // determine duration - for multiple strikes, add a little extra time float duration = (count == 1 ? script.lightningDuration : (script.lightningDuration / count) * UnityEngine.Random.Range(1.05f, 1.25f)); // make sure the duration lasts for all the lightning strikes script.lightningDuration *= count; // perform the strike Strike(intense, script.Generations, duration, intensity, script.ChaosFactor, script.GlowIntensity, script.GlowWidthMultiplier, script.Forkedness, count, script.Camera, visible ? script.Camera : null); // calculate the next lightning strike script.StartCoroutine(CalculateNextLightningLater(duration)); // thunder will play depending on intensity of lightning bool playThunder = (intensity >= 1.0f); //Debug.Log("Lightning intensity: " + intensity.ToString("0.00") + ", thunder delay: " + // (playThunder ? sleepTime.ToString("0.00") : "No Thunder")); if (playThunder && sounds != null && sounds.Length != 0) { // wait for a bit then play a thunder sound yield return(new WaitForSeconds(sleepTime)); AudioClip clip = null; do { // pick a random thunder sound that wasn't the same as the last sound, unless there is only one sound, then we have no choice clip = sounds[UnityEngine.Random.Range(0, sounds.Length - 1)]; }while (sounds.Length > 1 && clip == script.lastThunderSound); // set the last sound and play it script.lastThunderSound = clip; script.audioSourceThunder.PlayOneShot(clip, intensity * 0.5f); } }