- Home /
Randomizing Audio
I'm just gonna ask a quick question, If I have for example 6 sounds, how would I be able to randomize what sound should play every half second? I think it can be something like this so this is what I have so far:
var Sound1 : AudioClip;
var Sound2 : AudioClip;
var Sound3 : AudioClip;
var Sound4 : AudioClip;
var Sound5 : AudioClip;
var Sound6 : AudioClip;
function Update()
{
//randomize function here
}
//I think I have to make a repeat every second
@script RequireComponent(AudioSource)
I would be happy if I could get some help to understand randomizing etc.
Thanks!
Edit: #pragma strict var WalkSounds : AudioClip[];
function Update(){
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.S) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.D) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
{
audio.PlayOneShot(WalkSounds[Random.Range(0,WalkSounds.Length)]);
}
}
Try something like:
float timer = 0.0f;
public float timeInterval = 0.5f; // The time between each sound
function Update()
{
timer += time.deltaTime;
if (timer >= timeInterval)
{
timer = 0; // Reset the timer
// Do your random sound here
}
}
Answer by randomperson42 · Jan 02, 2015 at 12:23 AM
See http://answers.unity3d.com/questions/307478/randomly-play-audio.html and http://answers.unity3d.com/questions/413746/how-do-i-play-a-random-sound-each-5-seconds.html and https://www.google.com :)
Your answer
Follow this Question
Related Questions
Playing multiple sounds on same key input ("e") 1 Answer
GUI.Button Hover = Sound 1 Answer
Play sound upon Input in Javascript 1 Answer
Audio not playing [Fixed] 1 Answer
Sound playing at random. (JS) 2 Answers