- Home /
Sound on Collision script.
Hi everybody!
I recently set up a script to destroy a cube, when destroyed a particle effect will appear.. This all happens when the 'Player' enters its box collider.
The problem I'm having is that when I press Play the sound file that I set up for the cube plays straight away, and when the Player walks at the box the sound file doesn't play but the particle effect is still happening.
The code I'm using aint showing any errors.. But im thinking I could have it set up wrong.. Suggestions are welcome! =]
pragma strict
var Boom : ParticleEmitter; var onlyPlayOnce : boolean = true;
RequireComponent (AudioSource);
function OnTriggerEnter (collision : Collider) {
if (collision.gameObject.name == "Player")
{
Instantiate(Boom, transform.position, transform.rotation);
Destroy(gameObject);
}
}
private var playedOnce : boolean = false;
function OnTriggerEnter () { if (playedOnce && onlyPlayOnce) return;
audio.Play ();
playedOnce = true;
}
I dont know why exactly the code got put like this in my post.. apologies..
Answer by DaveA · Mar 16, 2012 at 01:06 AM
Check that your audio source is not set to play on start, see AudioSource.playOnAwake
You instantiate Boom but where do you destroy it? You probably want
var bang = Instantiate (Boom, transform.position, transform.rotation); Destroy (bang, .25); // or how ever long you want particles to emit
Answer by Foamy · Mar 16, 2012 at 02:21 PM
Cheers man, pretty helpful. =], Cheers man, pretty helpful =]