Taking Control of the Music

Blake Zoeckler
3 min readJun 21, 2021

--

Objective: add new music that plays when the boss appears, when the player dies, and when the player wins.

To achieve this, we’ll add MusicManager class that allows new music to play, fade in, and fade out. We include MusicData as a nested class, to better organize this in the inspector.

Here’s the inspector view of the script. We can attack .wav clips, volume, pitch, and fade in times for each music track we want to play. Anything that stays at -1 will instead use the default value of the audio source.

_defaultVolume and _defaultPitch are set in start, using the AudioSource values itself.

MusicManager.Start

We now create a coroutine that will cause music tracks to fade in, increasing their volume slowly over time instead of starting at full blast. The max volume and pitch are set to the default or the MusicData amount as necessary. Then every frame we slowly increase the volume using linear interpolation.

MusicManager.MusicFadeInRoutine
MusicManager public methods

Now to utilize this new coroutine, we add some public methods that are called from other classes as necessary.

OnDeath is called in PlayerLives when the player has died.

PlayerLives.OnDeath

OnBossAppear is called when the boss wave begins.

SpawnManager.BossWave

Finally, OnVictory is called when the boss dies.

BossLives.OnDeath edit

When the boss appears, we also want the current music to fade out. So we create another coroutine that linear interpolates the volume of the music to zero over some given amount of time.

Now there’s quite a bit more music that are all played at the proper times. The variety in music makes the game much more engaging.

--

--

Blake Zoeckler
Blake Zoeckler

Written by Blake Zoeckler

I’m a passionate and talented software engineer seeking an opportunity in game development.

No responses yet