Creators – Adding Sound

This week we spent some time adding sound to our game.

Recording

We needed a rolling noise and a falling pin noise. I worked on these sound at home.

img_3119

To get a rolling sound, I recorded several different objects rolling, but settled on a roll of duct tape rolling across a wooden table top. It had the pace and tone I was looking for.  For the falling/impact sound, I recorded a wooden muddler (visible above the duct tape above) falling over on the same wooden table.

The original sound files are available here.

Updating Our Sound Files For Game Use

We first loaded the rolling sound into Audacity.

Screen Shot 2016-10-27 at 20.33.20.png

The sound clearly had quiet portions at either end and the sound ramped up and down either side the relatively steady portion in the middle. We selected a reasonably steady central portion of the sound and removed the rest.

This sound was intended to loop seamlessly. Normally the start and end of a sound don’t line up well and there’s a clear point at which the loop starts again.

A useful property of this rolling sound is that sounds much the same backwards as forwards. We were able to take advantage of that to produce a smoothly looping sound. We copied the sound, pasted it to a new track, reversed the new track and mixed them together. This meant that either end of the new sound was identical and the looping was seamless.

With the impact sound, the process was simpler. The recording had a lot of silence at either end and an impact noise with two peaks:

screen-shot-2016-10-27-at-20-49-35

This had resulted from the muddler bouncing after the initial impact and coming down a moment earlier to hit the table a second time.

Since we only wanted a single impact noise, we just cut the sound to exclude the first impact.

In both cases these audio files were exported from Audacity as WAV files for use in Unity and we placed them in an Audio subfolder of our project. The edited files are here.

 

 

Adding The Sound Files To Our Game

We first worked on adding the rolling sound to our sphere. We wanted two things:

  • The sound should be louder as the sphere rolls faster
  • The sound pitch should rise as the sphere rolls faster

To provide a place for the sound to play from, we added an AudioSource component to our sphere and assigned the Rolling sound to it’s AudioClip property. We also made sure that the volume was set to zero and the “Play on Awake” and “Loop” options were both selected.

In our existing PushSphere script, we added two new public properties:

   public float VelocityVolumeFactor = 0.1f;
   public float VelocityPitchFactor = 0.1f;

these are used to relate the sphere’s speed to the sound’s volume and pitch.

We also added a private AudioSource property and got it in the Start() method:

   private AudioSource audioSource;
 
   // Use this for initialization
   void Start () 
   {
     :   :   :
     audioSource = GetComponent<AudioSource> ();
   }

Finally, in the Update() method, where it will be called every frame, we set the AudioSource’s pitch and velocity based on the sphere’s speed (a.k.a. the magnitude of the velocity):

   void Update () 
   {
     :    :    :
     audioSource.volume = rb.velocity.magnitude * 
                          VelocityVolumeFactor;
     audioSource.pitch = rb.velocity.magnitude * 
                         VelocityPitchFactor;
   }

We then tested the game. We found that the rolling sound is pretty quiet and isn’t always that apparent, but I was able to clearly hear it at home.

For things that make a sound when the sphere impacts them, we created a new script called SoundOnImpact. The code in this is very similar to the new code we added to PushSphere and it’s here in its entirety:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SoundOnImpact : MonoBehaviour 
 {
   public float VelocityVolumeFactor = 0.1f;
   public float VelocityPitchFactor = 0.1f;
 
   private AudioSource audioSource;
 
   // Use this for initialization
   void Start () 
   {
     audioSource = GetComponent<AudioSource> ();
   }
   
   void OnCollisionEnter(Collision collision)
   {
     audioSource.volume = collision.relativeVelocity.magnitude * 
                          VelocityVolumeFactor;
     audioSource.pitch = collision.relativeVelocity.magnitude * 
                         VelocityPitchFactor;
 
     audioSource.Play ();
   }
 }

Note that the code assumes that the game object this is attached to has both an AudioSource component and a collider of some kind. When a collision is detected, the sound is played. The volume and pitch of this sound are proportional to the impact velocity, in a manner similar to before.

We attached this script, and an AudioSource component, to both the walls and the pin prefab.

Project  Files

The full project files can be found here.

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s