- Home /
How to create sound on keyboard button?
Hi I have a game where I want it so when the arrow keys are clicked it emits whatever sound is assigned to the audio source attached to that object. It is a walking sound. And I need it to be in C# please. Thanks
Put something like this into your update function:
if(Input.GetAxis("Horizontal"))
{
audio.Play();
}
Answer by Hyperion · Mar 08, 2013 at 10:38 PM
The way it's done is you add an Audio component to your object. Choose the correct sound, and when you want it played:
using UnityEngine;
using System.Collections;
public class something: MonoBehaviour{
// setting up the audio component
audio.loop = true;
audio.playOnAwake = false;
if(Input.GetAxis("Horizontal")){
audio.Play();
}
else
{
audio.Stop();
}
}
But that's only if you have one Audio component. If you want your character to have multiple sounds, make a variable for the sound...
AudioClip: yourSound;
Then do
if(whatever)
{
AudioSource.PlayClipAtPoint(yourSound, transform.position);
}
Remember to drag your sound into the variable slot in the Unity inspector after coding. Although this second option is for non-looping sounds usually, you can fiddle around with it. I suggest using the first one for walking, but the second option for stuff like shooting. Please thumbs up my answer-Hyperion.
Thanks so much for the help, but when ever I use the first option it still gives me the error I was getting before.
Assets/AudioTrigger.cs(25,4): error CS0029: Cannot implicitly convert type float' to
bool'
That's really weird, since in my C# script it, the elements work perfectly. Of course, I bunched together multiple aspects that I use in different scripts...
So could you please tell me where line 25, space 4 is (what code it refers to)? That way, we can pin point the problem.
-Hyperion
It's pretty clearly referring to the bit where you try to use
Input.GetAxis("Horizontal")
as an if-statement conditional. Try replacing that with
if($$anonymous$$athf.Abs(Input.GetAxis("Horizontal")) > 0.2f)
(which checks if the axis is over a certain $$anonymous$$imum value) and see how you go.
Yes syclamoth's answer worked thanks, but now when I hold down the left or right arrows to move it just makes a buzzing noise likes its playing the sound really fast or something?
O.$$anonymous$$. There are two options.
-Option 1: $$anonymous$$ake a variable called walksounding and set it to false. Then, inside the if statement, only play audio if the walksounding variable is false. If it is false, play the sound, then set walksounding to true.
-Option 2: (at least I think that Get$$anonymous$$eyDown only happens once, so...)Change GetAxis("Horizontal") to Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow)||Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.DownArrow). If you want to change what keys you press, go to http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$eyCode.html for more key codes.
Your answer
Follow this Question
Related Questions
Audio Play Once 2 Answers
Play music trough scenes 0 Answers
How to make Audio "Range" Further. 2 Answers
Inputing an Equation 0 Answers