GameDev : Audiosource (Part III)!

In this article we set up the Asteroid Explosion sound.

Start with add Audiosource component to the Asteroid, toggle off Play on Awake, and drag the explosion sound in the Audio Clip field.
Exactly the same passagges we made for Laser clip.

Open up Asteroid script, take reference, NULL check, and assign clip into Inspector.
This is my result in Asteroid Inspector

Then inside OnTriggerEnter2D call
audiosource.Play()

I tried it and everything works fine.

But we need to manage the Explosion sound for the Enemy.
Again, same process for the other things.

I recommend to try it alone, is not so difficult, especially after we made this process together more than one time.
And even if you have some problems, DON’T WORRY.
We are here to try to become better, no one knows everything from scratch, we need practice to understand how things work.
Expecially in code.

Learn code or Unity is a continue process, more we make, more we understand. And more experience we have more good we become.

And for extra exercise, set up the sopund when we collect Powerups.

TIP : Powerups can be give you a little bug, if you can to make it alone is great!
But here I give you a tip.
When we call audioSource.Play() in Powerups, can be happen that sound don’t works.
This because we have the same problem than before : if the object is destroyed, the script is destroyed, so nothing happen

And we have another problem : when we collect it, we NEED to destroy immediately, we can’t let the Powerup goes around the screen even after we take it.

I give you one solution, is not the better, but it works.
After this solution, I give you another and it’s probably the best for this type of problems.

Objective : make the audio clip for the Powerups work.

FIRST SOLUTION : we can simply toggle off the SpriteRenderer.
So, after collect it, it disappear, we Play the sound, and NOW WE CAN USE A DELAY INSIDE DESTROY()
Because even if we set SpriteRenderer to false, if we destroy object nothing change…
To toggle off the SpriteRenderer, we need to take the reference to it, make a classic check for NULL, and before Destroy the object, Play the sound and make Spriterenderer = false.

SECOND AND BEST SOLUTION : use the static method
AudioSource.PlayClipAtPoint

https://docs.unity3d.com/ScriptReference/AudioSource.PlayClipAtPoint.html

With this solution, we don’t need the component AudioSource because the method is STATIC, and we can call it directly from the class.
AudioSource.PlayClipAtPoint()
Inside parentheses we need to pass the clip and the position. If we want, the third parameter is the volume, and goes form 0 to 1f.

Now, this solution is really good, but I PERSONALLY HAVE AN ISSUE.
The volume is incredible low.
I have been trying to find a solution, and for now I have found this.
I take the Main Camera reference and pass its position inside PlayClipAtPoint ()
I read something on some forum and it seems that the problem is the distance from the camera.
Honestly I do not know exactly how work this thing, but if I pass the Camera position the volume is completely fine.

Again, maybe this is a horrible solution, but it works so for now i use it.
But if I found something better I will write an article about it.