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

A little search would have easily given you the results, but nevermind. I am in a good mood. :slight_smile:

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!