This content originally appeared on Level Up Coding - Medium and was authored by Dennisse Pagán Dávila
In the previous article, I talked about how to add background music to your game. In this article, you will learn how to add sound effects to your game!
Playing Sound Effects
Objective: Add a sound effect that plays in response to a player action/ press of a button. In this example, I will be showing how to play a laser sound effect whenever the player fires/whenever the space key is pressed.
Process: I will go over two ways to achieve this:
∘ How to play sounds directly from a pre-set clip in the Audio Source Component
∘ How to play sounds by setting them through code
How to play sounds directly from a pre-set clip in the Audio Source Component
- Add an AudioSource Component to your object.
2. Drag-and-drop the desired audio clip to the respective slot from the Audio Source Component in the Inspector.
3. Make sure that Play On Wake and Loop are not checked.
4. Create a variable to reference the Audio Source.
4. Find the Audio Source component. You can directly find the component without having to access the object as long as this code is being written on the script attached to the object that has the audio.
5. Make a Null check for the Component. This is best practice whenever you get a component to make sure it has actually been found before trying to execute any logic involving it.
6. Now, you can access the clip from the Audio Source and play it at the press of a button.
How to play a sound clip by setting them through code
This process allows you to set the audio clip through code rather than have it pre-set through the Inspector in the Audio Source Component. This can be useful when needing to use different clips through the same execution. For instance, changing the laser sound if the player were to change their weapon to something else.
- Add an Audio Source component. However, the Clip section of the Component will remain empty for now as opposed to the previous process.
2. Create a global variable for the Audio Source and a global variable for the Audio Clip as well. The Audio Clip variable will be a SerializeField that will be assigned in the Inspector.
3. Set the Audio Clip in the newly created variable which is now visible in the Inspector.
Note: This does not go in the Audio Source Component.
4. In the Audio Source Component,make sure that Play On Wake and Loop are not checked.
5.Find the Audio Source component. You can directly find the component without having to access the object as long as this code is being written on the script attached to the object that has the audio.
6. Make a Null check for the Component. This is best practice whenever you get a component to make sure it has actually been found before trying to execute any logic involving it.
7. When you do the null check, you can take the opportunity to set the audio clip as well. If the component is found, set the Audio Clip using the reference you created in step 2.
Note: By using code to set the clip, if you have multiple audio clips to swap around, it could be done in this same logic through if-else statements, or better yet, switch cases, or arrays.
8. Now, you can play the audio at the press of a button.
Note: In my code, the audio is played in response to the player firing their laser weapon. Since light travels faster than sound, the statement is placed after the instantiation of the laser object.
My Sample Code:
Results
In the next article, I will go over How to Build and Test your Unity Game!
How to Play Sound Effects in Unity was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Dennisse Pagán Dávila
Dennisse Pagán Dávila | Sciencx (2021-05-12T17:02:20+00:00) How to Play Sound Effects in Unity. Retrieved from https://www.scien.cx/2021/05/12/how-to-play-sound-effects-in-unity/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.