Unity Development — Sound Effects

Christopher Graf
3 min readJun 11, 2021

--

To be fully immersed in a game, the player must believe everything they are seeing. As developers, we create every piece and add them one by one. While we can see all the pieces, the players should not. They should see one glorious experience. When our spaceship character shoots a laser, they should also hear a laser.

There are a few ways we can do this. Either way, there has to be an AudioSource component on either the Player, which is instantiating each laser, or the laser itself. We’re going to look at the Player option for now.

On the Player’s Inspector add an AudioSource. There are a lot of attributes, but the scaleable ones you should note are ‘Volume’ and ‘Priority.’ Volume should be pretty obvious. Priority matters when you have more than one audio track going at the same time. It determines which sounds take precedent over the others.

In the Player script, create a serialized AudioClip variable. Add the proper track for the laser shot in the Inspector. You are also going to need a private variable for the AudioSource. In the Start() method, be sure to assign the AudioSource with GetComponent().

Go to where the lasers are being instantiated in your code. Here is where you are going to put in the sound as it will match with the visuals. Because the Player is an object that may have multiple sounds throughout the game, we are not simply going to assign the laser sound in the Inspector. Instead, we have to assign the clip in code and then play it.

You can also make this slightly simpler with PlayOneShot().

Finally, there may come a situation where you play a sound and notice it is cut off. This is very lightly if you have an explosion sound or something similar from an enemy or some object being destroyed. A destroyed object cannot continue to play a sound. How do we solve this? With PlayClipAtPoint().

We don’t actually have to assign an AudioSource to whatever object is calling this method. The whole point of it is to play a sound after the local AudioSource is destroyed anyways. It simply plays the sound at a temporary point in space. You will most likely call it at transform.position, but with 3D projects, you can use this to your advantage to throw sounds and really set the atmosphere.

This this new knowledge with you and make the clip above even more spectacular.

--

--

No responses yet