Making some noises on music
Now that we have improved our game visually, with particles, we will add some sounds and a music in our project. It’s very simple in Unity but it’s still a very important part of a game.
You will learn where to find sounds and music, pick some, and play them in our game via a script.
Note: Unity 5 has been released with a lot of new Audio tools. However, we will not cover the new features in this tutorial.
Finding sounds and musics
Damien (not so) recently participated in an interesting subject on Stack Exchange about this topic.
Based on our knowledge, a game developer can:
- Buy sounds.
- Hire/Know a musician.
- Use free sounds from sound banks (like FindSounds or Freesound).
- Record his own sounds.
Or Damien’s favorite:
- Create chiptune (8-bit) sounds using BFXR (based on SFXR but with a web version that is very useful)
Matthieu: I have created some sounds and songs for a short school project made by a friend. I was a drummer at the time, but absolutely not a composer.
However, with the help of Freesound, a bit of inventiveness and a dozen of hours (without knowing any tool — so I just learned how to use Audacity the quick way), I successfully did a (cheap) soundtrack for the whole game.
I wouldn’t recommend that for a song (find a musician and make a deal, it’s way better), but with enough time and a good tool, you could definitely create cool sound effects. It’s feasible: be creative.
For musics, it depends of what you want:
- Jamendo has a ton of artists. Be careful with the licenses for commercial uses.
- Bosca Ceoil is a simple software from Terry Cavanagh to make musics
Damien: this is how I met (your mother) the artist Spintronic. I really loved his music. He kindly authorized me to use them in The Great Paper Adventure game.
Assets for the tutorial
Create or find an explosion and a shot sound. If you are lazy, you can use ours:
We will use one of the track of The Great Paper Adventure by Spintronic for this tutorial:
- Download Spintronic - Firecrackers
Import in Unity
Drag the 4 elements in the “Sounds” folder.
And… that’s all!
Playing music
To play a music, simply drag the song into the “Hierarchy”.
Observe the “Mute” checkbox. It can help when you do a lot of tests.
Playing sounds
You could proceed like for the music. But sounds need to be triggered at the right time in the game.
For that, we propose a simple solution. Just like for the “SpecialEffectsHelper” code, we will have a helper script for sounds that you can call from everywhere.
This new script is called “SoundEffectsHelper”:
using UnityEngine; using System.Collections; /// <summary> /// Creating instance of sounds from code with no effort /// </summary> public class SoundEffectsHelper : MonoBehaviour { /// <summary> /// Singleton /// </summary> public static SoundEffectsHelper Instance; public AudioClip explosionSound; public AudioClip playerShotSound; public AudioClip enemyShotSound; void Awake() { // Register the singleton if (Instance != null) { Debug.LogError("Multiple instances of SoundEffectsHelper!"); } Instance = this; } public void MakeExplosionSound() { MakeSound(explosionSound); } public void MakePlayerShotSound() { MakeSound(playerShotSound); } public void MakeEnemyShotSound() { MakeSound(enemyShotSound); } /// <summary> /// Play a given sound /// </summary> /// <param name="originalClip"></param> private void MakeSound(AudioClip originalClip) { // As it is not 3D audio clip, position doesn't matter. AudioSource.PlayClipAtPoint(originalClip, transform.position); } }
Add it to the “Scripts” game object, and fill its fields with the sound clips:
Then, do:
SoundEffectsHelper.Instance.MakeExplosionSound();in “HealthScript”, just after the particle effect.
SoundEffectsHelper.Instance.MakePlayerShotSound();in “PlayerScript”, just after
weapon.Attack(false);.
SoundEffectsHelper.Instance.MakeEnemyShotSound();in “EnemyScript”, just after
weapon.Attack(true);.
Start the game and listen. Yeah, we have a music and sounds now!
Note: this method is enough for the tutorial or for small projects. For a bigger game, it’s probably too light, as you won’t be able to easily manage hundreds of sounds.
Next Step
We just learned how to use sounds and music in our game.
We have a pretty good basis for a shmup game now. You can make a longer level and you will nearly get the demo we showed you in the introduction.
You could start making your own graphics and add enemies or background elements. You could even begin to implement a boss at the end of the level. It’s a bit more complex but the challenge and the design decisions are really interesting.
We are far from a complete game, but we will stop here for the gameplay and game stage.
In the next chapter, we will add menus so we can start and restart this hardcore level.
