x


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;

}

more ▼

asked Mar 15 '12 at 10:49 PM

Foamy gravatar image

Foamy
1 1 3 4

I dont know why exactly the code got put like this in my post.. apologies..

Mar 15 '12 at 09:57 PM Foamy
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Cheers man, pretty helpful. =], Cheers man, pretty helpful =]

more ▼

answered Mar 16 '12 at 02:21 PM

Foamy gravatar image

Foamy
1 1 3 4

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Mar 16 '12 at 01:06 AM

DaveA gravatar image

DaveA
26.8k 153 171 257

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2584
x42

asked: Mar 15 '12 at 10:49 PM

Seen: 584 times

Last Updated: Mar 16 '12 at 02:21 PM