How To Put Background Music In Unity
Playing Sound Effects
Objective: Add together a audio issue that plays in response to a player action/ press of a button. In this instance, I will exist showing how to play a laser sound effect whenever the player fires/whenever the infinite key is pressed.
Process: I will go over two ways to achieve this:
∘ How to play sounds directly from a pre-prepare clip in the Sound Source Component
∘ How to play sounds by setting them through code
How to play sounds direct from a pre-set clip in the Sound Source Component
- Add an AudioSource Component to your object.
ii. Elevate-and-drop the desired audio clip to the respective slot from the Audio Source Component in the Inspector.
iii. Brand sure that Play On Wake and Loop are non checked.
4. Create a variable to reference the Audio Source.
4. Find the Audio Source component. You can directly detect the component without having to access the object as long equally this code is existence written on the script attached to the object that has the audio.
five. Brand a Null bank check for the Component. This is best practice whenever you get a component to make certain it has actually been plant earlier trying to execute whatever logic involving it.
6. Now, you tin access the prune from the Audio Source and play it at the press of a push button.
How to play a audio clip by setting them through code
This procedure allows y'all to set the audio clip through code rather than have information technology pre-set through the Inspector in the Sound Source Component. This can exist useful when needing to use unlike clips through the same execution. For instance, changing the laser sound if the histrion were to alter their weapon to something else.
- Add an Sound Source component. Nevertheless, the Clip department of the Component will remain empty for now as opposed to the previous process.
2. Create a global variable for the Sound 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.
three. Set the Sound 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.Discover the Sound Source component. Yous tin straight 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. Brand a Null bank check for the Component. This is best do whenever you get a component to make sure it has actually been found earlier trying to execute any logic involving it.
7. When you do the nothing check, you tin accept the opportunity to ready the audio clip likewise. If the component is constitute, fix the Sound Clip using the reference you created in step 2.
Note: By using code to set the clip, if you accept multiple audio clips to swap around, it could be washed in this same logic through if-else statements, or better yet, switch cases, or arrays.
8. Now, you lot tin can play the audio at the press of a push.
Note: In my code, the audio is played in response to the player firing their laser weapon. Since light travels faster than audio, the statement is placed after the instantiation of the light amplification by stimulated emission of radiation object.
My Sample Code:
Results
How To Put Background Music In Unity,
Source: https://levelup.gitconnected.com/how-to-play-sound-effects-in-unity-6a122bb32970
Posted by: dustinhatiltas.blogspot.com
0 Response to "How To Put Background Music In Unity"
Post a Comment