- Home /
Playing an audio clip
I want an audio clip to play when two cars collide but I can't make it so the audio doesn't play at the beginning. If it doesn't play then, then it won't play at all. Any suggestions as to how I can script this. I can't figure out anything that doesn't give me an error.
Answer by Caiuse · Apr 22, 2011 at 11:15 PM
making sure theres an 'audio source' component attached to the object that has this script.
var myClip : AudioClip;
function OnCollisionEnter(collision : Collision) { audioClip = myClip; audioPlay(); }
I havent tested it, but it's along those lines.
- C
I kept trying variations of this code and eventually realized that an audio source component needed to be attached to the oject that has the script like you said. I also noticed that it had to be added before the script componenent was added. Thanks for that tip.
Answer by MC HALO · Apr 23, 2011 at 02:05 AM
hey if you want to play an audio source you can do the following:
var MyAudioClip : AudioClip;
function OnCollisionEnter (hit:Collision ){
AudioSource.PlayOneShot(MyAudioClip, transform.position );
}
if you want the sound to be 2 dimensional make sure that in unity when you drag the audio clip in to the inspector look for it and click on it. once you do that a list of options will become available on the sound look for where it says 3d sound tick that of and hit apply. The reason this is important is because if you leave 3d sound on and you move the player the sound starts to fade away when you are far away from the trigger :)
hope this helps.
Answer by IndieForger · Feb 08, 2013 at 02:37 PM
You might want to check this post if you have sound related issues.