- Home /
Play from a variety of sounds?
I have animations on my camera to give the effect of walking, on these animations I have triggers to tell a script when to play a footstep sound. This all works fine but I only have one footstep sound that gets played, I have three different sounds that I would like to be played randomly but I dont know how I would do this. This is my script can anyone help me out?
var footsteps : AudioClip;
function footstepsEvent(){
var footstepsEvent = new AnimationEvent();
footstepsEvent.functionName = "footstepsSound";
footstepsEvent.time = 0.0;
animation["gun walk"].layer = 1;
animation["gun walk"].clip.AddEvent(footstepsEvent); // Add the event to an AnimationClip
animation["gun walk"].wrapMode = WrapMode.Once;
animation.Play("gun walk");
}
function footstepsSound(){
audio.volume = 0.1;
audio.clip = footsteps;
audio.Play();
}
Answer by FLASHDENMARK · Mar 02, 2013 at 12:27 PM
var footsteps : AudioClip[];
function footstepsEvent(){
var footstepsEvent = new AnimationEvent();
footstepsEvent.functionName = "footstepsSound";
footstepsEvent.time = 0.0;
animation["gun walk"].layer = 1;
animation["gun walk"].clip.AddEvent(footstepsEvent); // Add the event to an AnimationClip
animation["gun walk"].wrapMode = WrapMode.Once;
animation.Play("gun walk");
}
function footstepsSound(){
audio.volume = 0.1;
audio.clip = footsteps[Random.Range(0, footsteps.length)];
audio.Play();
}
If I'm not mistaken that should do it.
Turn the footsteps variable into an Array, from which you select one randomly and then you play it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093657im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Play sound on animation event? 3 Answers
Can you fix my code? (Audio for doors) 2 Answers
Audio Play Once 2 Answers
Synch clips with BPM 2 Answers
Trigger diferent sounds playing from the same object 1 Answer