#100DaysOfCode#100DaysOfGameDevC#CodingGame DevScript-writingTrainingUnity

2D Game Development – Audio

Day 070 #100DaysOfCode
Day 025 #100DaysOfGameDev

Until now the game has been silent. That is now about to change! The Unity sound system has different parts:

BackgroundMusic Audio Source settings
  • Audio Clips – these are Assets, like textures or scripts, that are imported from audio files
  • Audio Listener – a component the defines where the “listener” is in the Scene. Especially useful with spatialized sound.
  • Audio Source – a component that allows the playing of an Audio Clip at the position of the GameObject that the component is on.

To begin getting audio into the game I started with background music. By creating an empty GameObject (named BackgroundMusic), I was able to add an Audio Source component to it and drag an Audio Clip into the AudioClip slot. Checking Play On Awake has the audio start playing as soon as it is loaded into the game and Loop is checked to keep it going at all times.

To get sound effects to play in the game a function of AudioSource will be used in the scripts called PlayOneShot. This function uses a given audio clip as its first parameter and will play that clip once, with all the settings of the Audio Source at the position of the Audio Source.

First, add an Audio Source component to the Ruby prefab, then edit the script:

preparing the RubyController script to be able to call audio clips

To get a pickup sound effect when Ruby collects a health pack, the HealthCollectible script gets a new public member of type AudioClip that will call the PlaySound function from the RubyController script:

Now, when Ruby grabs a health pickup, a sound clip is played to highlight the action.

The same is then done for when Ruby throws a cog and gets hit and takes damage by placing an appropriate PlayOneShot call at the right place in the code of the RubyController script:

added to ChangeHealth
added to Launch

Sounds until this point have all been 2D sound, which plays at the same volume no matter where they are placed compared to the source. This worked because they were all gameplay sounds that weren’t a part of the world itself. To get the sound of robots walking, things will have to be done a little differently now.

The Robot Prefab gets an Audio Source component and the Audio Clip gets set to Robot Walking Broken on Loop. The Spatial Blend slider gets pushed all the way to the right to 3D to make it a spatialized sound. The blue circle that appears around the speaker icon now represents the minimum distance of the spatial blend setting of the Audio Source. If the Audio Listener is inside that circle, it will hear the sound at its maximum volume. The sound will slowly attenuate until it reaches the maximum distance, where it will then be silent.

spatial sound settings

To fix attenuation due to the Camera (and its Listener) being above the plane of the game and far away from the Audio Source, a child GameObject is created under the Camera that is set on the ground and assigned a new Listener. The Camera Listener can then be removed and all audio will be heard directly below the camera’s position on the ground where it is sourced from.

Leave a Reply

Your email address will not be published. Required fields are marked *