How to play audio in Unity
As a Unity developer, there are times you have to work with sound effects for your game projects. Sound effects helps in enhancing the realism of your game. In this article you’ll be provided with the basics of playing audio sources in Unity.
How to create a sound effect
The first thing you have to do is to import the sound effects of your choice into your Unity project. Add the audio files to your project folder. The audio files should be in an appropriate format like mp3. Then, drag the audio files to the Unity Editor.
Now, create or select the GameObject you want to attach an audio source component to. Then, click on Add Component in the Inspector window and select Audio, then Audio Source.
With this, you’ve successfully added an audio source component to a GameObject. It’s now left to you to fine tune its properties to your desired taste.
Now that you’ve added an audio source component to a GameObject. How can the audio source be played?
There are different methods you can use to play a sound effect in Unity, but you’ll need to write a C# script.
Here are different methods to play a sound effect in Unity;
How to use the Play method to play a sound
To use the Play method to trigger a sound, create a new C# script and attach it to a GameObject in your scene with the Audio Source component.
using UnityEngine;
public class AudioPlayer : MonoBehaviour
{
private AudioSource audiosource;
void Start()
{
audiosource = GetComponent<AudioSource>();
audiosource.Play();
}
}
In the above script, the GetComponent method is used to access the Audio Source component attached to the GameObject. Then, the audio source is played with the Play() method. This method is handy for playing looping sound effects.
Using PlayOneShot
This method is useful when you want to play just a clip of the sound effect, not like Play that continues playing repeatedly.
Here’s an example:
using UnityEngine;
public class AudioPlayer : MonoBehaviour
{
public AudioClip audioClip;
private AudioSource audiosource;
void Start()
{
audiosource = GetComponent<AudioSource>();
audiosource.PlayOneShot(audioClip);
}
}
In the above script, the PlayOneShot() method is used to trigger the audio source. It takes in a parameter, which is the specific clip you want to play. This method is handy for playing short sound effects.
Using PlayClipAtPoint
This method is used to play an audio clip at a specific point in Unity. This allows you to play a sound effect without attaching an Audio Source component to a GameObject. All you need to do is to import the audio clip you want to play and attach it to your C# script.
Like this:
using UnityEngine;
public class AudioPlayer : MonoBehaviour
{
public AudioClip audioClip;
void Start()
{
audiosource.PlayClipAtPoint(audioClip, new Vector3(0,0,0)); //play the audio clip at position (0,0,0)
}
}
In the above script, the audio clip will be played at position (0,0,0). You can modify this position parameter to your desired position.
This method is handy for playing audio clips that are not attached to a specific GameObject.
Conclusion
Now, you have learned the different methods of playing an audio source in Unity. It’s now up to you to incorporate all you have learnt in your upcoming game projects.