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
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.