Tuesday, 24 November 2015

Working with Sound

Sound is a commonly overlooked aspect of game design that can help immerse a player in the world, become iconic to a certain scenario (like collecting an item) or alert the player to some danger. So today I will be looking at how sound can be implemented into Unity such is importing audio files, playing sound, making sound dynamic, triggering sound to play and manipulating volume.

With my facts supported by the official Unity website (Unity3d, 2015), firstly, importing any assets into Unity. It's a simply drag and drop affair from your PC's desktop into your project's content window, or by clicking 'Assets' at the top of the screen, then 'Import Assets'. However make sure your sound file is has a .wave, .aif, .mp3 or .off file extension for it to be compatible! When your audio is finally inside Unity, it will be replace by an 'Audio Clip' file (Docs.unity3d.com, 2015), which basically holds all the original sound data that you imported, with it being something an Audio Source object would use.
My own screenshot of the toolbar option in Unity












Now we have playing sound, which as taken from (Docs.unity3d, 2015), you can use the simple line of code "public void Play(ulong delay = 0);" in C#. All it does is plays your sound after the specified delay, so in this case the sound will play instantly as the delay is set to 0 seconds! Here's an example of code from the same source giving the programmer specific control over the playing of their sound: "using UnityEngine;
using System.Collections;

[RequireComponent(typeof(AudioSource))]
public class ExampleClass : MonoBehaviour {
    void Start() {
        AudioSource audio = GetComponent<AudioSource>();
        audio.Play();
        audio.Play(44100);
    } 
}"

Next is triggering the sound, where if you remember back to when I talked about collision that's one of the ways sound can be implemented here too. This can be performed by a simple trigger box and collision method, as sourced by (aldonaletto, 2015), from the Unity help forums (and not the official Unity blog). All you have to do is create a trigger volume that the player can collide with, then using this function code it will trigger the sound to play once: "
  1. function OnTriggerEnter(other: Collider){
  2. if (!audio.isPlaying){
  3. audio.Play();
  4. }
  5. }
" Following on from triggers, and triggering something from sound being played over collisions, you can also use an Audio Listener, (AudioListener, 2015), which works on a similar level to a built-in microphone inside Unity. Essentially any sort of audio input from the Audio Source will be able to set it off, playing the sound from the computer's speakers or headphones, and all you have to do is add the Listener into the game for it to work.

Now onto creating the sound dynamically, this can even be something as simple as playing sound from either the left or right speaker to make a game feel more immersive. It essentially means to play sounds and music in a creative or different way, so maybe play music inside a far-off caravan, enticing the player over to go and see it. Games in Unity should consider using an AudioSource GameObject that's attached to another object in-game, which (with information from (AudioSource, 2015)), is very useful to play background noises in games, and is especially useful when attached to the scene's camera object to help create looping ambient sound effects.

Manipulating volume just means the volume of your sound will be adjusted in your game, so a quiet whisper in a hedge will have a pretty low int value in your code, whereas a spooky jumpscare to freak out the player will set it abnormally high. This can be implemented by code, as used by (Kryptos, 2015) from Unity Answers. "
  1. // setting volume to zero
  2. audio.volume = 0;
  3. // or mute
  4. audio.mute = true;
" The first line lets you set the volume as loud as you'd like, where the second mutes it (so maybe in a scary game as the monster gets very close you mute the music entirely to get ready for a jumpscare!) These have a variety of uses and applications in Unity. If you'd like to manipulate your sound even further you can use an Audio Mixer (AudioMixer, 2015). Essentially it's a window within Unity that allows you to bled your sounds together, edit-in a variety of various affects to them and even adjust volume levels. All you really need to do is make sure the mixer is outputting to an Audio Source, and you can create a load of new sound effects to use containing within Unity, with you only using the existing ones you have as a base!

The Audio Mixer
A picture of the Audio Mixer tab open in Unity, from (AudioMixer, 2015)


No comments:

Post a Comment