During last week, I started adding music and sound effects to our group game, Motherland. I wanted a way for music to transition in smoothly when accessing new areas and overlaying on existing music.
This official tutorial video demonstrated how to do this by attaching the audio source to the in-game camera as a child:
I started by sourcing two pieces of royalty free music / sound effects, in this case I wanted the a strong wind sound to be playing in the background, whilst a stealth-sounding music clip to overlay when accessing patrolled areas.
From here, I created an object child of the main camera and named it “Music Player”, further applying two “Audio Source” components. This is where I assigned my two .wav music files I sourced earlier.
I ensured the “play on awake” and “loop” options are selected, as I require the music to be continuous and begin when the level loads. Also, manipulating options such as stereo and pitch to the desired levels.
Using Unity’s built in audio mixer, I started to route the previously imported sound files as two children (wind and action), through the “master” channel. This meant these two audio sources could be directly controlled from a single channel to keep audio levels at a constant.
Next, I created snapshots of my sound levels by creating custom settings which I required at certain situations within the game.
For example, this is the setting at the beginning of the level, when you are outside an exposed to the elements with no enemies in sight.
In comparison, this is the sound levels when the players enters an area still exposed to the elements, however there are enemies presents and the player has to stealthy.
This provides the player with an audio clue, as well as visual, that there is an enemy in the vicinity and they will have to adjust the game-play to suit.
Lastly, I created trigger boxes and tagged them as “CombatZone” to make them easier to reference when coding. These will be the areas which will trigger when certain audio snapshots are to play.
Now it was time to begin creating the code to link all these components together and provide the transitions between music levels.
Firstly, I created some public variables to allow me to select the my audio mixer snapshots and references them. Also, an audio clip array which would be a variation of sounds that play whilst transitioning. Lastly, creating a public float which links into the bpm (beats per minute) of the music to allow for smoother transitioning.
The private floats will be used to calculate the speed in which music transitions in / out based on the bpm.
NOTE: You will need to know the bpm of the music tracks you are using and enter them manually.
Next, I created the code which identifies when the player accesses the trigger boxes I had labelled “CombatZone”, to begin transitioning in the relevant music. Also, to play a selected sting, however I decided to leave this section out for now whilst I was testing.
Then, I added code which provides a transition when exiting the “CombatZone” trigger box, which will return to the standard background sound (wind).
I then repeated the above process to create numerous collision trigger boxes to provide a variation of different music levels to play across different areas of the game.
This was then compiled and saved.
I then applied the “BackgroundMusic” script I had just created to my player model, which in this case was a sphere whilst I was testing. I then assigned my audio snapshots to relevant conditions I wished for them to transition in. Also, making sure I had applied the rigid-body component to allow for collision detection.
The final test is shown below. Take note how the audio levels of the wind and stealth music fluctuate across the scene.
All that remains is to transfer this code into our main game and adjust the transition and audio levels to match the scenes.