- Home /
Press Button for Sound
Hello,
I'm trying to develop a goal type game (Fps), Swat type, and i need a script where when you press J It plays the sound (Like SwatTalking.mp3) So the person says "Police on the floor!", Could someone please provide me with a script?, So basicly on input the talking plays.
And if possible, I have 4 voices saying different things, 'Police on the floor' 'Hands up!' 'Police!' 'Get down!' things like that, i need it that everytime you press J It plays one of them, so not in order, just random one every time you press the button,
I hope someone can provide me with a script,
Thank you
Dude, never say "someone give me a script" in UA - most guys here become infuriated with these (bad) magic words! You're lucky today: @OrangeLightning is in a good mood and provided it without complain (so good that he posted the answer twice!)
But in the future, ask for it in a more generic way, like "could anyone point me the right direction?" - you'll probably receive the script anyway, but without some furious guys downvoting you and eating your reputation.
Answer by FLASHDENMARK · Oct 11, 2011 at 12:08 PM
A little search would have easily given you the results, but nevermind. I am in a good mood. :)
First and foremost the easiest is to have an array of audio files and play them one of them randomly when you press the J key.
var swatCommands : AudioClip[]; // Add your audio files here in the inspector.
function Update (){
if(Input.GetKeyDown("j")){
audio.PlayOneShot(swatCommands[Random.Range(0,swatCommands.length)]); // Choose a random clip here.
}
}
@script RequireComponent(AudioSource) // Make sure the object has a AudioSource attached.
It should work. I may have spelled length wrong, but the compiler should tell you if I did.
That is properly more or less the basics of randomly choosing a file from an array.
Good luck!
Yeah I know. Is that a bug? Because I swear I did not press that button twice.
But I cannot delete it because of some bug on UA I guess.
I deleted the duplicated answer. It happened to me a few times too... but at least in my case it wasn't a UA bug - it was a bug of $$anonymous$$e!