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, 2010 at 03:04 PM

Graeme gravatar image

Graeme
53 25 19 26

(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, 2010 at 03:16 PM

Mike 3 gravatar image

Mike 3
33.2k 21 88 297

(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:

x6395
x1725
x1062
x93
x76

asked: Jun 22, 2010 at 03:04 PM

Seen: 2382 times

Last Updated: Jun 22, 2010 at 03:04 PM