- Home /
Trigger sound when walking through cube trigger
hi guys, all i want to do is have a specific sound triggered when i walk through a box with the trigger setting on. I have searched hard for this and found many answers but i just cant seem to implement it in my scene! :(
Answer by reptilebeats · Mar 25, 2012 at 12:31 AM
if i understand right all you want is a sound to play on trigger this is very simple all it is
function OnTriggerEnter(){
audio.Play(); }
or if the audio is on another object use gameObject.Find("other").audio.Play();
Answer by DaveA · Mar 24, 2012 at 05:23 PM
sry Dave, but here's the whole script even :)
http://unity3d.com/support/documentation/ScriptReference/AudioSource.PlayOneShot.html
cheers for the quick reply! :) sadly this is the one i have tried countless times. i throw it into the javascript editor, then throw that into the cube and it appears in the inspector panel. once i have done that i throw in the audio file to the script in the inspector then play, walk through the cube and still no sound :(.
would the type of audio format be a problem?! they are .wav files
maybe the the sound IS playing, but your audio listener is too far away for you to hear it ?
http://unity3d.com/support/documentation/Components/class-AudioListener.html
OR , do you have an Audio Source attached to the gameObject that the play script is attached to ?
http://unity3d.com/support/documentation/Components/class-AudioSource.html
Create an Empty gameObject, give it the component Audio Source (untick play on awake, and leave the audio clip box at None / empty). Then apply the script. (Don't forget to add the sounds to the prefab / object in the inspector after you have attached the script and component audio source).
From another answer I gave : http://answers.unity3d.com/questions/210047/questions-about-sound.html
still does not work for me, tried them all :( everything looks like it should work! this is tre annoying, will have to give up have been trying to figure this out for a good 28 hrs to no avail. thanks anyway
that is so bizarre dude ....
@$$anonymous$$icheal Gray raises a good point :
OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider . http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html
OnTriggerEnter is called when the Collider other enters the trigger . http://unity3d.com/support/documentation/ScriptReference/Collider.OnTriggerEnter.html
try some debugging. where you have the command to play audio => print("audio.Play CALLED"); see if you are getting a message in the console when the sound is meant to be played.
Answer by Himo · Mar 29, 2012 at 04:09 PM
I found that when you apply the code to the first person controller the steps you have to untick the play on awake box and then apply the code to the first person controler. Maybe this will help?
Your answer
Follow this Question
Related Questions
Sound On Collision Not Working 3 Answers
Best Technique For 3D Cube Map 0 Answers
Cant repeat Sound 1 Answer
Something wrong with destroying script 0 Answers
Wait until audio is finished before set active is false 2 Answers