Audio and Accessibility

Play audio clip when a trigger is entered

You don’t have access to this lesson
Please register or sign in to access the course content.
Length: 30 minutes|Difficulty: Standard

This lesson involves moving a player character[1] into a collider area which is set to a trigger and using this to trigger an event[7] to play a selected audio clip.

First set up a level with a player character on it.

Next create a GameObject or Prefab that we want to play a sound when we get close to it. In this example it is a red sphere called WarningGlobe.

If we play the scene and move close to the globe nothing happens. We haven’t added any triggers yet.

We want to add a trigger to the warning globe. We could set the existing sphere collider to be a trigger. However this will mean that we have to touch the existing collider first.

We will add a box collider to the WarningGlobe object. We use a box collider as it is less memory intensive that a sphere collider.

Next we need[8] to resize the box collider.

Click on the Edit Collider Button to do this.

We can now either click on the green squares to resize each side or enter change the size and center values in the inspector.

Here you can see that we have changed the x and z dimensions to 5. We have left the y collider 1 as we are not worried about detecting the y axis.

Check the is trigger toggle.

Return to your scene.

You should now be able to see the new trigger collider when you select the object.

Now we need to add a script to determine if we have interacted with the trigger. There is another example of how to do this in the Unity: Essentials lesson Reload Scene.

Create a new script. The one in this example is PlayAudioOnTriggerEnter.

Attach the script to the object with the trigger. In this example WarningGlobe.

Open the script.

Add a call to the OnTriggerEnter function[2]. This takes a Collider parameter[3]. This is the collider of the object that has hit the other object (in this case[9] our player).

Inside the function we have initially put a Debug.Log statement to print out a message so we can check the trigger is working.

	void OnTriggerEnter(Collider other) {
        Debug.Log("Hit something");
    }

Return to Unity and test your scene.

Here you can see the debug message is printed when I enter the 5 unit range[10].

No we need to modify the script so that it only triggers for the player object (or any other object we choose).

Fist select the player object.

Note here the Tag is set to untagged.

Change this to Player (or another tag of your choice).

If you don’t see the tag that you want, select add tag.

Click the + and add the tag you want. Make sure to click Save.

Go back to the script.

Inside the OnTriggerEnter function replace the debug line of code with:

	void OnTriggerEnter(Collider other) {
        if(other.CompareTag("Player")) {
            Debug.Log("Hit the player");
        }
    }

This will now only run if an object with the player tag enters the trigger area.

Test your program.

Now that the trigger is working correctly, we need to add audio to play when we enter the trigger area.

Add the Audio Source component[4] to the game object with the trigger.

Find an audio clip / file that you wish to play.

You can download a beep bop boop mp3 here for your testing[5]. This is the sound used in this example.

If we play the scene now the sound will play immediately.

Uncheck the Play on Awake checkbox on the audio source.

Now we need to edit our script so that when we enter the trigger we play this audio source.

We need to add the AudioSource object and give it a variable[6] name. As this is private to this class we start the variable name with an _. Here the variable is named _soundEffect.

We also want to be able to assign data[11] to this variable in the Unity editor. To achieve this we use the [SerializeField] property before the variable.

We also add the .Play() command to the variable as shown in line 7 below. This will play the sound effect associated with this audio source variable.

	[SerializeField]
	AudioSource _soundEffect;

    void OnTriggerEnter(Collider other) {
        if(other.CompareTag("Player")) {
            Debug.Log("Player in range");
            _soundEffect.Play();
        }
    }

If we run the program now the scene runs but we get an error in the console when we enter the trigger.

This is because we haven’t set what audio source to use.

Click on the target and select the Audio Source you wish to use. Alternatively drag the audio source into the box.

Test your program.

Congratulations you now have a working audio source effect that responds to a trigger.

Terms definitions
2. function. A named section of a computer program that performs a specific task. Functions help make code more efficient and reusable. They may take input parameters and produce output.
3. parameter. An extra piece of information passed to a function to customize it for a specific need
4. component. The three strands of Technology in the NZC (Technological Practice[12], Technological Knowledge, and Nature of Technology) – together with the six specialist knowledge and skills strands for levels 6, 7, and 8) – support[14] the development of students’ technological literacy[13]. Each of these strands is broken down into smaller groupings (or substrands) called “components”. For […]
5. testing. Testing[5] is about confirming decisions and ensuring that the desired result is produced form and outcome[16] or component.
Terms definitions
1. character. Length: 30 minutes|Difficulty: Standard This lesson involves moving a player character into a collider area which is set to a trigger and using this to trigger an event to play a selected audio clip. First set up a level with a player character on it. Next create a GameObject or Prefab that we want to […]
2. function. Length: 30 minutes|Difficulty: Standard This lesson involves moving a player character into a collider area which is set to a trigger and using this to trigger an event to play a selected audio clip. First set up a level with a player character on it. Next create a GameObject or Prefab that we want to […]
3. parameter. Length: 30 minutes|Difficulty: Standard This lesson involves moving a player character into a collider area which is set to a trigger and using this to trigger an event to play a selected audio clip. First set up a level with a player character on it. Next create a GameObject or Prefab that we want to […]
4. component. Length: 30 minutes|Difficulty: Standard This lesson involves moving a player character into a collider area which is set to a trigger and using this to trigger an event to play a selected audio clip. First set up a level with a player character on it. Next create a GameObject or Prefab that we want to […]
5. testing. Length: 30 minutes|Difficulty: Standard This lesson involves moving a player character into a collider area which is set to a trigger and using this to trigger an event to play a selected audio clip. First set up a level with a player character on it. Next create a GameObject or Prefab that we want to […]