- Home /
Help with random melee noises?
Previously I recorded five different audio tracks for sounds that the player makes when stabbing. Know i just need help with coding some thing that selects a sound to play at random.
var sound1: AudioClip;
if (Input.GetButtonDown("Fire1"))
{
// let me select a noise at random
audio.PlayOneShot(sound1);
{
Answer by Unitraxx · Mar 30, 2013 at 09:42 PM
Make a List with all the audio clips in, and then randomly generate an index.
http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
Not sure if my code will work, I'm not used to JS, but it should give you the basic idea. This first part, should be outside the Update()/OnGUI() function you are using.
var sound1: AudioClip;
var sound2: AudioClip;
var sound3: AudioClip;
var sound4: AudioClip;
var sound5: AudioClip;
var sounds = [sound1,sound2,sound3,sound4,sound5]
I think you know where you should place this :
if (Input.GetButtonDown("Fire1")){
audio.PlayOneShot(sounds[Random.Range(0, sounds.length)]);
}
EDIT : As Chronos-L pointed out, the max value of Random.Range isn't included in the interval of random numbers. So I changed it to Random.Range(0, sounds.length)
.
thanks but could you explain more thoroughly, still a indeter$$anonymous$$ate coder...
To add on @unitraxx's answer, you can modify it to have any amount of random melee sound you want:
var sound: AudioClip[];
...
if (Input.GetButtonDown("Fire1")){
audio.PlayOneShot(sounds[Random.Range(0, sounds.length)]);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
audio different in game than preview? 1 Answer
Sound won't PLAY!!!!!!!!!!!!!!!!!!!!!!!!!!!! 1 Answer
How would I make a laser beam sound? 1 Answer