- Home /
Activating Audio When Hitting a collider
Okay HEYYYY i was just wondering how i would activate an audio source when hitting a collider i thought it would be like the code below but it does not seem to work :/
function OnTriggerEnter (Hit : Collider)
{ AudioSource.Play("Name Of the Audio"); {
This Method doesn't Work And i was wondering if somebody could help, or point me in the right direction :D thanks for reading :D
Ins$$anonymous$$d of OnTriggerEnter, try OnCollisionEnter.
Answer by GoThXoRd · Feb 29, 2012 at 12:00 AM
Im reasonably new to coding, but maybe try changing
AudioSource.Play("Name Of the Audio");
to
audio.Play();
and add an audio source to the object and uncheck 'loop' and 'play on awake'
not too sure, hope this helps.
Answer by Alec-Slayden · Feb 29, 2012 at 09:22 AM
First, make sure you close the function with a closing bracket }, not another open bracket. Next, as GoThXoRd mentioned, you should use
audio.Play();
with the audio on the game object (you can just drag it on in the inspector, even).
If you are still having troubles, the following must be true for that function to work:
- Both game objects must have colliders
- One game object must have a rigidbody (See the bottom of this page)
- One of the colliders must be a trigger
- The physics collision layers should be on
If you don't want the collider to be a trigger, use OnCollisionEnter()
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Vehicle help 1 Answer
Animation stoping!! 1 Answer
Audio not playing [Fixed] 1 Answer
Code Error!!!! 2 Answers