As a student of Unity I am  fascinated by the easiness and the promise the engine brings with it. In this blogpost I want to discuss about types of Sound playing in Unity.

Sound Playing Types?

Yes sound playing types. As far as I have seen there are three occasions we play a sound:

  • Continuously – as in Soundtrack or Background Music
  • Creation sound – like when we shoot a laser or spawn our player
  • Ending sound – like an explosion, where we show and destroy something and play a sound accompanying that animation

Let’s begin with the easiest one:

Continuous Sound Playing

To make a sound effect play continuously, all we have to do is add an Audio Source to our Camera and place a sound to its Audio Clip and set its Play On Awake and Loop to true

This will make it play when the game starts and loop it when it ends.

Creation Sound

Another type of sound effect is one when we play after we created, instantiated, an object. The basic example to that is firing a laser when we press Space.

This requires some logic work though, but the essential component is the same: Audio Source

First of all, let’s answer the question about who is going to carry this sound? Well, whoever responsible for creating the entity. Because, if we carry on with our example, it is the spaceship that makes that voink sound while emitting the laser particles.

So our venerable spaceship gets an Audio Source and it carries a laser emitting sound within it. Also notice that its Loop and Awake On Play settings are false:

Because we don’t want it to play when the game starts AND we only want to play it once when we press space. This phrasing suggests we should use some logic – hence the script where we hold player behavior should enter into the picture and carry this piece of code wherever we spawn our laser:

// First the handler to AudioSource we placed on our Player
private AudioSource _laserSound;

// then on start we should populate this variable with actual component on our Player
void Start()
{
        _laserSound = GetComponent<AudioSource>();
}

// Finally play the sound when we instantiate our laser
void Update () {
        Movement();

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Instantiate(_laserPrefab, transform.position + new Vector3(0, 2, 0), Quaternion.identity);
            _laserSound.Play();
        }
}

Ending Sound

Finally, the destruction sound, the sound which accompanies a destruction animation as an example. At first glance the previous solution would be useful in here too. But this code which I did write when I was learning about it didn’t work:

 private void DestroyExample()
    {
        _explosionSound.Play();
        Destroy(this.gameObject);
    }

Why? Because before it has a chance to play, I destroy this gameobject along with the audio source.

The solution? Well, it seems counter-intuitive but it has a logic in itself. We carry the sound in the behavior itself as an AudioClip and when we destroy our object we use a function called PlayClipAtPoint() which does exactly like that – plays a sound in the location we give to it as parameter:

// This is filled from Inspector panel in Unity
[SerializeField]
private AudioClip _explosionSound;

 private void DestroyExample()
    {
        Destroy(this.gameObject);
        AudioSource.PlayClipAtPoint(_explosionSound, Camera.main.transform.position, 1.0f);
    }

This plays an _explosion sound in the place where our Main Camera is and with a sound of 1. I explicitly give it my main camera because Unity tries its best to render a 3d sound and sometimes if you pass transform.position here it gives a very low sound, however if you pass it our camera, it will be much louder. And because it is passed to a static function of our AudioSource class, it doesn’t get affected from destroying this object.

Categories: Unity