- Home /
Random Audio Clip Help
Can you help me make a script where 7-8 audioclips play at random. Repeating is fine.
Answer by aldonaletto · Aug 29, 2011 at 11:51 PM
You can create an AudioClip array, then use Random.Range(0,audioClipArray.length) to select the sound randomly (Random.Range(0,max) draws an integer from 0 to max-1):
var sounds: AudioClip[]; // set the array size and fill the elements with the sounds
function PlayRandom(){ // call this function to play a random sound
if (audio.isPlaying) return; // don't play a new sound while the last hasn't finished
audio.clip = sounds[Random.Range(0,sounds.length)];
audio.Play();
}
NOTE: Add an AudioSource to the object owner of this script.
Very helpful, thank you. I'd +1 this but I have inadequate rep for that.
Answer by vestax_ion · Aug 29, 2011 at 11:48 PM
ok, but maybe you have to use your brain abit and it will take an hour! in the process you will learn ALOT!
look at the play sound script in the UNITY REFERENCE... figure out how it works and make 7 8 variables for playing your sound... http://answers.unity3d.com/questions/12546/playing-audio-clip.html
Secondly look at the RandomRange variable...you use this to select randomly things. by making a random number from one to 8 http://unity3d.com/support/documentation/ScriptReference/Random.Range.html
Then... look at ARRAYS!!!! fill an array with the varables for 7 sounds....
and then make a button like.... if jump button then play random sound from 1 to 8. and when you have it loading, then change jump to whatever trigger you want.
var blocks : GameObject[]; ///declare an array except use soundobjects
var ino = Instantiate(blocks[Random.Range(0, blocks.Length)], displace2 + displace, rot);///
use a line like this except instead of instantiate, say ... PLAY sound 1 to 8... blocks length is 8 if you have 8 sounds. using the oplay commands,
that is all, 3 lines, figure out how to use code!!!
you can alternatively declare and fill an array with this kind of logic:
var array2 = new Array (Vector3(0, 0, 0),Vector3(0,0,-90),Vector3(0, 0, 180),Vector3(0, 0, -270) , Vector3(90, 0, 0) ,Vector3(-90, 0, 0),Vector3(0, 0, 0),Vector3(0,0,90),Vector3(0, 0, -180),Vector3(0, 0, -270) , Vector3(90, 0, 0) ,Vector3(-90, 0, 0));
Answer by dar0307 · Jul 29, 2014 at 11:13 PM
i'm sorry, i'm trying get a little help, but if you can't help me... fine no problem :D