Created
September 13, 2022 11:02
-
-
Save Feargrieve/8be6b878048c2e1ce05b132132270d3a to your computer and use it in GitHub Desktop.
Lightning Spawner Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class SpawnLightning : MonoBehaviour | |
| { | |
| public Vector3 spawnCentre; | |
| public Vector3 spawnArea; | |
| public float minTimeDiff; | |
| public float maxTimeDiff; | |
| public int numberOfStrikes; | |
| public GameObject lightningPrefab; | |
| public GameObject MainCam; | |
| public AudioSource lightningAudioSource; | |
| public AudioClip lightningClip1; | |
| public AudioClip lightningClip2; | |
| public AudioClip lightningClip3; | |
| public float volume = 0.5f; | |
| private int randomSoundInt; | |
| void Start() | |
| { | |
| StartCoroutine(SpawnLightningRandomly()); | |
| } | |
| IEnumerator SpawnLightningRandomly() | |
| { | |
| for (int i = 0; i < numberOfStrikes; i++) | |
| { | |
| Vector3 randomSpawnPositionLeft = spawnCentre + new Vector3(Random.Range(-spawnArea.x / 2, spawnArea.x / 2), 1, Random.Range(-spawnArea.z / 2, spawnArea.z / 2)); | |
| Instantiate(lightningPrefab, randomSpawnPositionLeft, Quaternion.identity); //randomly spawn lightning in area around spawn center left | |
| randomSoundInt = Random.Range(1, 4); | |
| Debug.Log(randomSoundInt); | |
| if (randomSoundInt == 1) | |
| { | |
| lightningAudioSource.PlayOneShot(lightningClip1, volume); | |
| } | |
| if (randomSoundInt == 2) | |
| { | |
| lightningAudioSource.PlayOneShot(lightningClip2, volume); | |
| } | |
| if (randomSoundInt == 3) | |
| { | |
| lightningAudioSource.PlayOneShot(lightningClip3, volume); | |
| } | |
| yield return new WaitForSeconds(Random.Range(minTimeDiff, maxTimeDiff)); //random time diff length between spawns | |
| } | |
| } | |
| private void Update() | |
| { | |
| //make lightning face active cam | |
| lightningPrefab.transform.LookAt(MainCam.transform); | |
| } | |
| private void OnDrawGizmosSelected() //gizmo for showing spawn area left, right & endzone | |
| { | |
| Gizmos.color = new Color(1,0,0,0.5f); | |
| Gizmos.DrawCube(spawnCentre, spawnArea); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment