How to Create a Simple Audio Impact/Collision Trigger with Unity

Objectives:

  1. Introduce Unity’s basic audio components/settings

  2. Construct a simple C# script to play a sound when two objects collide

 

To begin, open a new unity project. For this tutorial we will be using 2018.3, but everything we’re doing here will work with most versions of Unity. Start by adding a plane at (0,0,0), a sphere at (0,4,0) and moving the camera to (0,3,-8).

Next we will set up the necessary audio components.

Audio Listener

The Audio Listener represents the player’s ears and is placed on the main camera by default. In general it is best to leave the Audio Listener in its default position, so that’s what we’ll do here. Importantly, there may only be one Audio listener in a scene at a time, but you can preview the audio from any position using the audio preview button in Scene view.

Default Location of the Audio Listener 

 

Audio Source

Unity’s Audio Source is arguably the most important audio component in any project. The Audio Source is attached to a game object and allows for audio clips to be played and paused and have their volume adjusted. For this project we will place an audio source component on the sphere. This can be done by selecting the sphere then going to Component > Audio > Audio Source. Once its added un-check Play on Awake so that the sound doesn’t automatically play when we run the scene.


Next up we will need a sound to fill the AudioClip field. Since this sound will be triggered by the sphere colliding with the ground (the plane), this sound should be some sort of short impact sound. Here we will be using a sound from our 8-bit Essentials sound pack.

First, drag the audio file into the Assets folder in the Project window of unity. After it loads, select the sound and, in the Inspector window, make sure that you check “Force To Mono” then click apply. This isn’t completely necessary, but is a good habit to get into for sounds such as impacts that you may want to spatialize. This would not apply to non-diegetic sounds such as music or narration, for example.

 

Editor configuration for the impact sound

 

Next, add the sound to the Audio Source that is on the sphere by dragging it onto the Clip section.

Clip added to Audio Source

 

Rigidbody

Now we will make it so that the sphere can fall and impact the ground. For this we will add a Rigidbody component to the sphere by selecting the sphere and going to Component > Physics > Rigidbody. Make sure that Use Gravity is checked.

 

Rigidbody

Rigidbody component, applied to the sphere

 

We now need to write a C# script to play the impact sound when the sphere hits the ground. You can do this by selecting the sphere and going to Add Component > New Script. Name the script something like “Impact Trigger” and then open it in some sort of text editor or IDE (I’m using Visual Studio). First we’ll get rid of Unity’s default update function and add an AudioSource variable, named source. Then, in the Start function, we’ll link our AudioSource variable to the audio source component by using GetComponent<AudioSource>(). The script should look like this:

 

    AudioSource source;

    void Start()
    {
        source = GetComponent<AudioSource>();
    }

To trigger the sound we will use the OnCollisionEnter() function, which is called whenever the object that this script is applied to collides with another object. Note that, for this to work, both objects mush have collider components, but this isnt a problem for us because both the sphere and plane have colliders applied by default. Write this function like this:

 

private void OnCollisionEnter(Collision collision) 
{ 
    source.Play();
}

 

Overall, the code should look something like this:

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ImpactTrigger : MonoBehaviour
{
    AudioSource source;

    void Start()
    {
        source = GetComponent<AudioSource>();
    }

    private void OnCollisionEnter(Collision collision)
    {
        source.Play();
    }

}


Thats it! You should have a working impact trigger at this point. When you press play, this is what you should see:

 


 

Thanks for reading. Click the menu tab for more Unity tutorials, sound packs, and software.

hello