i made a 3rd ps and have a script to destroy a target when hit by a bullet prefab which is attached to a gun carried by a char animated by mixamo. the char raises his gun and fires the bullet prefab which explodes the target on impact but for some reason i cant get a sound of an explosen to play when the target explodes. I have tried a couple of scripts on got on the forum but they dont work. Whats the best way to set this up so the explosen makes a sound when it goes off when hit with the bullet prefab.
var explosionSound : AudioClip;
var explosionPrefab : GameObject;
function OnCollisionEnter (thing : Collision) {
if(thing.name == "Bullet") { //just for example I'm using names, generally it's better to use tags
var sound = Instantiate(explosionSoundPrefab, transform.position, Quaternion.Euler(0,0,0));
sound.GetComponent(AudioSource).PlayOneShot(explosionSound);
//blow up
}
}
The explosion prefab is like a fire, explosion, or just a plain AudioSource, but it needs an AudioSource. This is usually is okay if your effects last long enough, but in cases they don't, create a separate object that only plays the sound. When it's done, destroy it.