- Home /
play music on key press
I want to play a sound when I pressed number 1 key on the keyboard, how do I do it?
Answer by djm · Jun 20, 2012 at 06:28 PM
Try the following script on the gameObject that has the Audio Source. Look at the inspector window and you will see an inspector variable in the script. Here you can select "Alpha1" for the number 1 key. You can add the Alpha1 into the code to but having an inspector variable gives you control over the key input incase you wanted to add more sounds played by different keys.
var keyBoardSound : KeyCode;
function Update ()
{
if(Input.GetKeyDown(keyBoardSound))
{
audio.Play();
}
}
had a similar issue*
I attempted 20 solutions, none worked...
I use this scrip and works perfect!!!
I can map the hole keyboard with this puppy...
THAN$$anonymous$$S!!!! A $$anonymous$$ILLON!!!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
play music on key press 1 Answer
Help; I have two code errors need adivce 0 Answers
I need help with a companion!!! 2 Answers
End game then an Object is close to another object? 0 Answers