- Home /
Audio. How to play a sound file when triggered by collision?
Hi All
I'm a total beginner. I've no experience of scripting. I've looked through a few tutorials and still not sure what I'm doing.
In the image I've made a ramp. I want an audio file to play if the player falls off the ramp and triggers the box collider below. I've attempted a script but it doesn't work when I play. Any advice greatly appreciated... Thanks
pragma strict
var myClip : AudioClip;
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "First Person Controller"){
audio.PlayOneShot(myClip);
}
}
How you do get the same audio clip to stop playing when you leave the trigger zone?
Answer by DaveA · Feb 14, 2013 at 02:19 AM
You would need an AudioSource component on that same object for audio.Play.. to work. Or make another object with and AudioSource, set the clip to it, get that object, tell its audio to play.
Answer by RENDERCLIPPS · Feb 14, 2013 at 03:04 AM
Excellentamundo Thanks so much Dave
No problem. But this should have been a comment, now it looks like you got 2 answers!
Your answer
![](https://koobas.hobune.stream/wayback/20220613092649im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Cannot get trigger sound to play for collectibles 1 Answer
Play Animation OnTriggerEnter 2 Answers
OnTriggerExit stop audio 1 Answer
Objects Touching? 1 Answer
Detecting object which is going through inside circle particle system 1 Answer