Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity Game Development Blueprints

You're reading from   Unity Game Development Blueprints Explore the various enticing features of Unity and learn how to develop awesome games

Arrow left icon
Product type Paperback
Published in Nov 2014
Publisher
ISBN-13 9781783553655
Length 318 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
John P. Doran John P. Doran
Author Profile Icon John P. Doran
John P. Doran
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. 2D Twin-stick Shooter 2. Creating GUIs FREE CHAPTER 3. Side-scrolling Platformer 4. First Person Shooter Part 1 – Creating Exterior Environments 5. First Person Shooter Part 2 – Creating Interior Environments 6. First Person Shooter Part 3 – Implementing Gameplay and AI 7. Creating Save Files in Unity 8. Finishing Touches 9. Creating GUIs Part 2 – Unity's New GUI System Index

Adding in sound effects/music

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:

  1. Select your enemy prefab, and add an Audio Source component to it by selecting Component | Audio | Audio Source. An audio source lets the audio listener into Main Camera knowing that this is an object that can play sounds. Once you create the Audio Source, open the 3D Sound Settings and change the Min Distance of the Volume Rolloff to 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.
  2. After this, let's go into our 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;
  3. Next, go into the CollisionEnter2D function. After the Destroy(theCollision.gameObject) line, add the following code:
    // Plays a sound from this object's AudioSource
    audio.PlayOneShot(hitSound);

    Note

    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:

  1. Back in the inspector for our enemy, let's set the 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!
  2. Go to the Explosion prefab, and add an Audio Source component in the same way we did in step 1. Once you create the audio source, open up the 3D Sound Settings, and change the Min Distance of the Volume Rolloff to 10. After this, set the Audio Clip property in the component to the explode sound.
  3. Now, going back to our 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.

  4. Finally, I want to play a sound whenever we fire a shot. To do that, let's go to 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.
  5. Next, go into PlayerBehaviour, and add in a new variable, as follows:
    // What sound to play when we're shooting
    public AudioClip shootSound;
  6. After this, whenever we shoot a bullet, let's play the new sound at the beginning of the ShootLasers function:
    audio.PlayOneShot(shootSound);
  7. Coming back to Inspector, set the Shoot Sound property in the PlayerBehaviour component to the shoot sound effect.
  8. Finally, let's add in our background music. Go to your Main Camera object in Hierarchy. Add an Audio Source component. There's no need to set Min Distance in this case, because this object is where the audio listener is. Change Audio Clip to bgm, check the Loop option, and set the Volume to .25.

    Note

    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/.

  9. Save everything, and run the game!

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.

Note

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime