How to Play Sound Effects in Unity!
Objective: Adding sound effects to your game.
Sound effects can be powerful additions to your game. Sound effects be satisfying, like the sounds of getting coins in Mario, or can be infuriating, like the dog in duck hunt. Sounds can make you feel a whole range of emotions and today I’m going to show you how to implement sounds into your games.
To begin, I’m going to show you how to add a sound effect to you player, whenever he shoots a laser.
As we have learned from the previous tutorial we need and audio source, we also need a variable to represent our audio clip.
Add the audio clip to the reference on the inspector.
Now we need to create a handle for the Audio Source.
Don’t forget the null check
This time we need to add and else statement. Use this to set the Audio Source clip to your audio variable.
Now when do we want this sound effect to go off. Whenever we fire our lasers. Under the method you use to instantiate your laser is where you will place the sound effect.
That’s it, now whenever you fire your laser you will hear the sound you’ve chosen to play go along with it, breathing more life into your game. This is a simple way to add sound effects to actions in your game. Remember, there are multiple ways to do everything in Unity, So I’ll show you one more way to give your actions sound.
Together we have created an explosion prefab. We can add sound to the prefab so every time the explosion animation is called it will play the audio with it automatically.
Select your explosion prefab and add the Audio Source component.
Attach the audio file you wish into the audio clip location on the inspector.
Check the box for Play On Awake.
Now, every time you call for this prefab it will play the explosion animation and the sound effect together. With these methods you can now add sound effects to your game and take your adventures to the next level. Remember, don’t be afraid to experiment with your code, and I’ll see you in the next tutorial.