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.
  2. From there, go into the Inspector and uncheck the Play On Awake property. Otherwise, if there is something attached to AudioClip it will play that sound automatically.An audio source lets the audio listener attached to the Main Camera object know that this is an object that can play sounds.
  3. 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 hit
    public AudioClip hitSound;
  4. Next, go into the CollisionEnter2D function. After the Destroy(theCollision.gameObject) line, add the following code:
    // Plays a sound from this object's AudioSource
    GetComponent<AudioSource>().PlayOneShot(hitSound);

    In this instance, I used the GetComponent function to access the AudioSource component that is attached to the game object this is attached to. It is important to note that GetComponent is a fairly expensive function to call. Since this will only be used once, it's OK to use like this; but if you were to use this more than once, I would suggest creating an AudioSource variable and setting it in the Start function instead like we will be doing in the next section.

    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.

  5. Now, we need some actual sounds to play. I've set aside a folder of assets for you in the Example Code folder, so open your project folder via your Explorer and move the folder into your project's Assets and then return to Unity.
  6. 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!
  7. Go to the Explosion prefab, and add an Audio Source component in the same way we did in step 1; however, make sure that the Play On Awake property is still checked. After this, set the Audio Clip property in the component to the explode sound.

    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 check Play On Awake or 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.

  8. 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.
  9. Next, go into PlayerBehaviour, and add in two new variables, as follows:
    // What sound to play when we're shooting
    public AudioClip shootSound;
    
    // Reference to our AudioSource component
    private AudioSource audioSource;
  10. Then, we need to initialize the audio property, so add a Start function with the following:
      void Start()
      {
        audioSource = GetComponent<AudioSource>();
      }
  11. After this, whenever we shoot a bullet, let's play the new sound at the beginning of the ShootLasers function:
    audioSource.PlayOneShot(shootSound);
  12. Coming back to Inspector, set the Shoot Sound property in the PlayerBehaviour component to the shoot sound effect.
  13. Finally, let's add in our background music. Go to your Main Camera object in Hierarchy. Add an Audio Source component. Change Audio Clip to bgm, check the Loop option, and set 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/.

  14. Save everything, and run the game!

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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset