- Home /
Random Audioclip. No Repeat ??
I am trying to create random footsteps with no two clips repeating. Maybe a Unity Javascript equivalent to shufflebag or something. Could someone help me preferable by adding to this code (I'm a bit of a noob programmer...!)
public var stepSounds : AudioClip[];
function PlayRandomSound() {
if (stepSounds.length>0) { var idx : int = Random.Range(0,stepSounds.length-1); audio.clip = stepSounds[idx]; audio.pitch = Random.Range(0.9, 1.1); audio.Play(); } }
Answer by Mike 3 · Jun 22, 2010 at 03:16 PM
Something like this should do it:
public var stepSounds : AudioClip[]; private var stepShuffleBag : Array = new Array();
function PlayRandomSound() { //fill the shuffle bag if it's empty if (stepShuffleBag.length < 1) { foreach(var clip in stepSounds) stepShuffleBag.Add(clip); } //random range should be from 0 to length, it's exclusive of the top value
var idx : int = Random.Range(0, stepShuffleBag.length); var audioClip = stepShuffleBag[idx]; stepShuffleBag.RemoveAt(idx); audio.clip = audioClip; audio.pitch = Random.Range(0.9, 1.1); audio.Play(); }
Your answer
Follow this Question
Related Questions
Random footsteps 5 Answers
Footstep Audio Check Collider Hitting Floor 1 Answer
Footsteps script not working 2 Answers
Foot step audio- randomize pitch 2 Answers