-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Unity Game Development Blueprints
By :

Another thing that we can do to give the project a little more polish is add in sound effects and background music. Let's do that now. Perform the following steps:
10
. Unity will attempt to alter the volume and pan sounds to make the project sound nicer, but for this project, it just makes everything much softer, so we're going to undo the effect unless it's far away.EnemyBehaviour
script! As usual, we'll need to add in a new variable for us to use to play whenever we're hit:// What sound to play when we're hit public AudioClip hitSound;
CollisionEnter2D
function. After the Destroy(theCollision.gameObject)
line, add the following code:// Plays a sound from this object's AudioSource audio.PlayOneShot(hitSound);
For more information about the PlayOneShot
function, check out http://docs.unity3d.com/ScriptReference/AudioSource.PlayOneShot.html.
For more information on the Audio Source component (audio), check out http://docs.unity3d.com/ScriptReference/AudioSource.html.
Now, we need some actual sounds to play. I've set aside a folder of assets for you in the Example Code
folder, so drag-and-drop the Sounds
folder into your project's Assets
folder:
Hit Sound
variable in the EnemyBehaviour
script to the hit sound that we've imported by using drag-and-drop. Now if we play the game, when we hit an enemy, the sound will be played! Now, let's have a sound if we destroy the enemy!10
. After this, set the Audio Clip property in the component to the explode
sound.EnemyBehaviour
script, go to the line after we instantiate the exploder
object, and add the following line before we destroy the exploder:exploder.audio.Play();
Now, if you play the game, hitting the object will play one sound, and when the object is destroyed, the explosion will play a sound. Because the sound is in the Audio Clip property, we can just call the Play
function. However, if you want an object to play multiple sounds, it's better to have separate AudioClip
variables just as we did with EnemyBehaviour
.
playerShip
and add an audio source. Once you create the audio source, open 3D Sound Settings, and change the Min Distance of Volume Rolloff to 10
.PlayerBehaviour
, and add in a new variable, as follows:// What sound to play when we're shooting public AudioClip shootSound;
ShootLasers
function:audio.PlayOneShot(shootSound);
PlayerBehaviour
component to the shoot
sound effect..25
.The background music is provided for this project by Stratkat (Kyle Smith). If you are interested in more of his work, check out his website at http://daydreamanatomy.bandcamp.com/.
While we won't see any changes at this point for those of you actually running the game, you'll notice quite a change when the game is started. It's already feeling much more like a game.
If you don't want to deal with the 3D settings, you can also select the sound files and uncheck the 3D sound option, but this will give you less control.
Change the font size
Change margin width
Change background colour