Triggering Voiceover Clips OnTriggerEnter

Marcus Ansley
2 min readJun 14, 2021

--

The third-person stealth game I’ve been working on has some voiceover clips which need to be played when the player reaches certain positions. One of the quickest and easiest ways we could achieve this is through GameObjects with BoxCollider triggers and a custom VoiceOverTrigger script.

Objective: Play AudioClips once when the player enters certain areas.

1. Set up some empty GameObjects with BoxCollider components on them. Position and scale these until they’re in the correct positions in the level (most likely ensuring the player can’t miss them). Make sure to set the ‘isTrigger’ boolean on the collider component to true in order to prevent collisions.

2. Create a new script (named ‘VoiceOverTrigger’ perhaps) and add it to these GameObjects.

3. Set up the script to have a public or serialised private AudioClip. You’ll also want a private bool to keep track of whether the clip has already been played (‘_played’ here). You can also have a public AudioSource component and drag this in via the inspector, but in my case I’m going to be using one on the main camera (which I’ll locate automatically within the Start method).

4. Create a ‘OnTriggerEnter’ method, checking to see if the object/’other’ which has entered the trigger’s radius has the “Player” tag. Check to see whether the clip has already been played and, if it hasn’t play it through the AudioSource of your choice. You can also do some null-checking here to make sure there is indeed a clip and an available AudioSource.

I initially tried using AudioSource.PlayClipAtPoint on the main camera, but found I ran into an issue whenever the camera needed to change position: the audio clip would be playing in the original position of the camera rather than its current one. As such, I’d personally recommend playing this clip through a dedicated AudioSource 😉

5. Head back into Unity, let it compile/reload, and then drag the AudioClips you want to use into the available slots/serialised variables in the Inspector window.

Assuming the player character has a collider of some description, a RigidBody, and the appropriate “Player” tag, the OnTriggerEnter method should get called when the player walks through one of these colliders, playing the appropriate AudioClip on cue 😙

--

--

Marcus Ansley
Marcus Ansley

Written by Marcus Ansley

Game Developer | Game Design and Literature Graduate

No responses yet