In this tutorial we’ll add music to the game.
It doesn’t matter if you want to add actual music or just some environmental tracks – your game needs some noice!
We’ll set up multiple music clips and add some of them to the game world, while others will be used later (e.g. battle and victory music).
Learn more about music in this Makinom documentation.
Music Setup #
Navigate to Game > Music, we’ll change the Default music and add additional music.
You can find all the music audio clips in Assets/Tutorial Assets/Audio/Music/cynicmusic – in case you want to check out other tracks from the same composer, check out cynicmusic’s other assets on opengameart.org. Most of them are available under CC0 license.
0: Town Theme #
- Name
Set to Town Theme. - Audio Clip
Select TownTheme. - Loop Clip
Enable this setting.
1: Forest Theme #
- Name
Set to Forest Theme. - Audio Clip
Select GameMusic_ForestTheme_24. - Loop Clip
Enable this setting.
2: Battle Theme #
- Name
Set to Battle Theme. - Audio Clip
Select battleThemeA.
Loop Settings #
We’ll use a loop within the track, i.e. at a certain position we’ll change the playback position back to a previous one.
Click on Add Loop.
- Use PCM
Enable this setting. - Check PCM
Set to 4086000. - Set PCM
Set to 607000.
You can test loops directly in the editor via the Test Loop button. The Game view has to be unmuted for this to work.
3: Victory Theme #
- Name
Set to Victory Theme. - Audio Clip
Select Heavy_ConceptB. - Loop Clip
Disable this setting.
The victory theme should only play once.
4: Outpost Theme #
- Name
Set to Outpost Theme. - Audio Clip
Select 025_A_New_Town. - Loop Clip
Enable this setting.
5: Dungeon Theme #
- Name
Set to Dungeon Theme. - Audio Clip
Select song18. - Loop Clip
Enable this setting.
6: Boss Battle Theme #
- Name
Set to Boss Battle Theme. - Audio Clip
Select battleThemeB.
Loop Settings #
Click on Add Loop.
- Use PCM
Enable this setting. - Check PCM
Set to 2868000. - Set PCM
Set to 0.
That’s it for the music setup.
Don’t forget to save your changes by clicking on Save Settings at the bottom of the editor.
Scene Setup #
We’ll add our music to the game using Music Player components.
You can add them either via the scene hierarchy context menu (Makinom > Music Player) or the Makinom scene wizard (Create Game Object > Music Player).
There are different variations available:
- Music Player
Creates a music player that plays the music on Start, e.g. when the scene is loaded. - Music Player 2D
Creates a music player that plays the music when the player enter’s the 2D trigger of the music player. - Music Player 3D
Creates a music player that plays the music when the player enter’s the 3D trigger of the music player.
We’ll use the start and 3D trigger variations.
Start Menu #
Let’s start with the Start Menu scene (Assets/Scenes/) – open the scene and add a Music Player (i.e. not 2D or 3D variants) using the context menu or scene wizard.
Change the following settings in the Music Player component.
- Play Type
Select Stop.
The start menu will stop any playing music when we return to it.
Dungeon Level 1 – 5 #
For the Dungeon Level 1 to Dungeon Level 5 scenes (not the Dungeon Battle Arena), we’ll also use the Music Player that plays automatically. The dungeon scenes can be found in Assets/Scenes/Dungeon/.
- Play Type
Select Fade Out Play.
This’ll fade out the current music and play the music player’s track afterwards. - Music Clip
Select Dungeon Theme. - Fade Time
Set to 1. - Interpolation
Select Linear.
The setup is the same for all 5 levels, so you can either copy/pase the game object or create a prefab out of it and use that.
World #
Open the World scene (Assets/Scenes/), this time we’ll use the Music Player 3D, which has a Sphere Collider added, used as a trigger.
We need to place multiple music players to play our town theme, forest theme and outpost theme. The forest theme will be added multiple times, to cover playing it when leaving town, the dungeon and the outpost. Remember how the scene looks like:
Town #
Add the music player to the town are and increase the Radius of the Sphere Collider to cover a large part of the town – at least the spawn point and part of the bridge should be covered. I’m using a radius of 60.
For better organization, change the game object’s name to Music Player Town.
As for the Music Player component:
- Play Type
Select Fade Out Play. - Music Clip
Select Town Theme. - Fade Time
Set to 1. - Interpolation
Select Linear.
We’ll also add a condition to it, we don’t want to use the music player while being in battle – otherwise we accidentally change the music when intersecting with the music player’s trigger.
In the Music Player‘s Condition Settings:
Click on Add Condition.
- Condition Type
Select Game State.
Click on Add Game State.
- Condition Type
Select Game State. - Game State
Select In Battle. - Check State
Select Inactive.
Outpost #
Add the music player to the outpost area and increase the Radius of the Sphere Collider to cover the outpost, mainly the entrance. I’m again using a radius of 60.
Change the game object’s name to Music Player Outpost.
As for the Music Player component:
- Play Type
Select Fade Out Play. - Music Clip
Select Outpost Theme. - Fade Time
Set to 1. - Interpolation
Select Linear.
We need to add the game state condition to the Condition Settings again.
Click on Add Condition.
- Condition Type
Select Game State.
Click on Add Game State.
- Condition Type
Select Game State. - Game State
Select In Battle. - Check State
Select Inactive.
Forest #
We’ll add multiple music players for the forst, first, let’s add one at the outpost, since we’re already in that area. A radius of 10 is enough this time, place it at the outpost’s exit.
Change the game object’s name to Music Player Forest Outpost.
As for the Music Player component:
- Play Type
Select Fade Out Play. - Music Clip
Select Forest Theme. - Fade Time
Set to 1. - Interpolation
Select Linear.
We need to add the game state condition to the Condition Settings again.
Click on Add Condition.
- Condition Type
Select Game State.
Click on Add Game State.
- Condition Type
Select Game State. - Game State
Select In Battle. - Check State
Select Inactive.
Copy the game object and move it to the dungeon entrance. Make sure it’s also placed at the correct height.
Change the game object’s name to Music Player Forest Dungeon.
Copy the game object and move it to the town’s exit. Make sure it’s also placed at the correct height.
Change the game object’s name to Music Player Forest Town.
Testing #
And that’s it – save the scene.
Hit play and you’ll now hear the town theme when spawning, switching to the forest theme when leaving town, etc.
Next, we’ll set up areas.
(Optional) Organizing the scene #
For even better organizition of your scene, add an empty game object, rename it to Music and parent all music players to it.
Keeping your scenes tidy and uncluttered is always a good idea.