Play audio clip when a trigger is entered
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 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 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. This takes a Collider parameter. This is the collider of the object that has hit the other object (in this case 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.
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 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. 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 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 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.