2D Shooter: Pew Pew!

Objective: Add pew pew sounds whenever the lasers fire.

Let’s start in the player script and add two variables: One for the audio clip, and another for the audio source component.

Now in the inspector, attach the audio file you want to _laserShot.

Now let’s get the audio source component in the start method.

Now we need to check if the audiosource is null, and if not, to connect the audio clip to it.

We could have the audio file within the Audiosource itself, and that way we would only need to get the component and check its status, but this way we have room to add other clips down the line.

Now to call the sound when the lasers are instantiated.

All you need is that last line of code: “_audioSource.Play();”



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sabrina Windsor

Currently learning to code with the help of GamedevHQ in order to someday my my game ideas come to life!