- Home /
Random Array of sounds for Random Game Object
I want to Randomly create an array of Thunder Sounds to play when the GameObject comes into scene.
In other words, I have a Random Lightning script that works perfectly to summon a Lightning.Prefab into the scene, this prefab also play a single audio with it, but I want it to play a random array of thunder sounds instead.
I am sure we would need to put this on the the Lightning Randomizer Script, so here it is:
#pragma strict
//Objects
var Lightning : GameObject;
var Box : GameObject;
//Box Attributes
private var BoxLength : float;
private var BoxWidth : float;
private var BoxHeight : float;
//random Values
private var randX : float;
private var randZ : float;
private var randY : float = BoxHeight;
//timer settings
private var timer : float = 0.0;
var Mult : int = 0.1;
function Update () {
//seting up boundrys
BoxLength = Box.gameObject.transform.localScale.x;
BoxWidth = Box.gameObject.transform.localScale.z;
BoxHeight = Box.gameObject.transform.localScale.y;
//setting up the random values
randX = Random.Range(0,BoxLength);
randZ = Random.Range(0,BoxWidth);
//bringing the lightning in and randomizing the x and z
timer -= Time.deltaTime*Mult;
if(timer <= 0){
Instantiate(Lightning);
Lightning.transform.position.x = randX;
Lightning.transform.position.z = randZ;
timer = 1.0;
}
}
Answer by cdrandin · Aug 10, 2013 at 01:06 AM
Look at this. http://answers.unity3d.com/questions/52017/2-audio-sources-on-a-game-object-how-use-script-to.html
Simply make an array of audio sources. Randomize between the array from 0 to length of array - 1.
Then play it, you should consider pausing when to play the audio or else you will get a lot of noises.
Ok, that will work, but now for the finale, how would I make that audi travel to the player for realistic thunder SFX? Would I have to make an empty game object with the sound array, and attach that to the Lightning.PREFAB, if so, what type of script would I use if the player is tagged Player?
Your answer
Follow this Question
Related Questions
Making an Array of Animations Play at Random With No Repeats 3 Answers
Simple array an spawning question 6 Answers
Choosing a random GameObject out of an array? 1 Answer
Using arrays to randomly select an object 0 Answers
Calling random functions 4 Answers