- Home /
Best way to play audio sound effects
Hey guys,
I always wondered what the default way is to playback audio Sound Effects, because my current way seems kinda overly complicated.
Here is how I do it currently:
I have a GameObject, with an AUDIO.js script. I link every AUDIO source from the scene in there like that:
var EnemyHitSound : AudioSource;
static var enemyHit = false;
And I set up the Update function like that:
function Update () {
if (enemyHit){
EnemyHitSound.Play();
enemyHit = false;
}
}
So when I want to play the Audiofile from any script I just write: AUDIO.enemyHit = true;
This seems like such a hassle and completely counter intuitive. Is there a more straight forward way of being able to play any Audio Source in the scene, from any script?
I really appreciate your help.
Thanks
Look at the Broadcast set of functions in the Reference guide
Answer by devilkkw · Jan 28, 2012 at 01:33 PM
var EnemyHitSound : AudioClip;
static var enemyHit = false;
And I set up the Update function like that:
function Update () {
if (enemyHit) audio.PlayOneShot(EnemyHitSound);
}
}
whi you don'y use "onCollisionEnter" function?is must fast.check the guide.