Play an audio when a particle is spawned - Unity3D

Hey,

I am working on a meteor shower effect. A curve is controlling the emission of the particles (meteors)

How can I trigger a sound effect every time it emits a particle? as well as when it collides on a mesh.
Oh, Btw I am using Unity 4.7.2f1

Cheers,
Parag

On Collision should be simple. Long time no Unity, so i’m not sure about emission though.

1 Like
  1. use “collision on” in your particle system.
  2. use collider on the desired surface to be hit.
  3. create empty object where you attach your sound component to.
  4. create script that uses “On Collision” and get the sound component in the code (GetComponent).
  5. in the function , if collision on just sound.play().
  • that is the fastest way I could simplify for you , there are many other better OOP designs to make it work depends on the your project.

cheers,

1 Like

I was wondering how can we enable it per particle.

Else, as my programmer friend suggested I have to give up the emission curve and emit a single particle so that he can instantiate the particle system and attach a sound to it.

Which will take away the freedom of randomly generated meteors :’(

So, what you actually want is a flying meteor, having a sound attached to it, after spawn? Then one sound playing on impact?

I would always create a prefab for the meteor then. With effects and sound and all. You can still spawn meteor prefabs via script randomly then. Random spawning in c# is one line of code :wink:
When using a separate prefab per meteor, you can also attach a collider to your prefab (which is adjustable in size and position) and get your collision events through the OnCollision of said collider, no need to use the particle collision then.

1 Like

Yeah … That’s what he suggested. I ll try that.
Let’s see how it works.

I guess I’m not clear with my explanation but if you place the emitter inside the collider I mentioned in line 2) with out mesh renderer so it will basically play sound every time a particle is birth.

it should work easily.

1 Like

Oh interesting, Let me try it out :slight_smile: Smart, Sorry I couldn’t understand it at first. :kissing_heart:

1 Like

no problem bro :slight_smile: if you want when a birth then have collider for that. and collider for the hit surface.
let me know if it worked for you.

cheers!

1 Like

Sure thing, I’ll just try it out. I am not good at writing a script at all so it might take a little time.

I’ll keep you and @TobiasTobasco posted about the progress.

Thanks guys :slight_smile:

    void OnParticleCollision(GameObject colliderForBirth, Sound birthSound) {
    Collider birth = colliderForBirth.GetComponent<Collider>();
    if (birth) {
       birthSound = GameObject.find("the emepty object with the sound").GetComponent<Sound>();
       birthSound.Play().
    }
}

Here for the kick start, I’m bored so I did for you in five second. that should live inside the script you attach to the GameObject with the PartcileSystem.

Good luck,

1 Like

Thanks for the script.

But there is one issue with this, I have created one invisible plane in front of my Meteor emitter, I already have collision ON for it, Now the particles are colliding with the invisible plane and exploding there (as I have other emitters on collision).

P.S. Particle trigger is the invisible plane and Particle collision is the collision mesh where I want it to explode.

:frowning:

easy fix , go to the collider component and select “Triggered”

1 Like

Awesome, It worked.

Let me try the script :kissing_heart:

but let me ask you a question regarding the whole technical architecture.
why you use particles as meteors? why not each meteor is an object which is modeled with a mesh . and particles will only be for the fill-up and effects? so it will be much better to performance be calculated only once by the CPU to create the meteor object and destroy it. instead of giving the GPU to calculate the particle behavior every frame ?

what is the goal of this project?

If I create them as a mesh how can I shoot them in random direction?

This is not very important part of the project, It is just an environmental FX.

so I would suggest it to be efficient and smart.
model a meteor , or go to Houdini to generate some random models.
sit down with your programmer to design the code architecture and I would suggest to simplified it in the way @TobiasTobasco mentioned build a nice prefab that contains your particle system and the model with nice material and textures, and the script for the collision but this time it will talk about game object collision and not particles.

then with easy code you can instantiate the meteor prefab with a sound for birth and destroy it on hit with sound. its really easy and cheap performance wise. obviously you can control which direction it spawn that very simple code.

I guess you wanted to control the style of birth but I don’t think particle system are meant for that kind of goal.
live your artistic side for the nice textures. ribbons, derrises, light for heat and this kind of stuff to fill -up around that meteor.
and of course as well for the explosion of hit.

1 Like

if you mean “just an environmental fx” like just to fill up in the background so particle system can do that. I thought at the beginning the focus is on the meteor like zoomed with detailed… but here I gave you two options. :slight_smile:

cheers,

1 Like

Thanks for the detailed explanation, I 'll give it a try and update what worked the best for me. :slight_smile:
It is just an environmental FX just to create some dramatic feel in the world :slight_smile: