x


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(); 
  } 
}
more ▼

asked Jun 22 '10 at 03:04 PM

Graeme gravatar image

Graeme
53 9 9 15

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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(); 
}
more ▼

answered Jun 22 '10 at 03:16 PM

Mike 3 gravatar image

Mike 3
32.4k 17 78 278

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5407
x1478
x886
x84
x64

asked: Jun 22 '10 at 03:04 PM

Seen: 2080 times

Last Updated: Jun 22 '10 at 03:04 PM