- Home /
Trigger Script on AudioListener
I'm trying to make a small function which resides on an object with an AudioListener so that I can print text at the location of the audio source. It's so that I can print a cue when any sound that is picked up by the AudioListener is played - it would help deaf people play games.
Answer by clunk47 · Sep 18, 2013 at 12:15 AM
Sounds like you want this text to be in 3D space, correct? Have a look at TextMesh. Also take a look at how to add 3D Text, which uses a TextMesh component. To enable the mesh by trigger, you could parent it under the collider which has a trigger attached, or add a trigger collider to the mesh itself, as well as your AudioSource. Either way, have a look at OnTriggerEnter.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
void OnTriggerEnter(Collider c)
{
if(c.gameObject.GetComponent<AudioSource>())
{
if(!c.audio.isPlaying)
c.audio.Play();
}
else
{
print ("No AudioSource Attached To Collider!");
}
}
}
I'm confused as to how a trigger collider would fire on playing audio; I thought collisions are game objects intersecting.