How can particle emitted when collider hit an object?

Hello everyone!
I’m trying to create a new script for doing something with particle system.

This script start with following details:

function OnControllerColliderHit(hit:ControllerColliderHit){
	
	if(hit.gameObject.tag == "findo"){
		
		Destroy(hit.gameObject);
	}	
}

I put an object its tag named “findo”,now the character can destroy the object when collider hit.
But the most important is I want a particle I’v created immediately play (when hit and object destroyed)
I don’t know how to do :cry: I’v tried so many hours and just couldn’t get it!
Can someone help me solve this problem? I have no idea with scripting.

THANKS!!

Hello,

Try something like this,

Create a prefab of your particles then instantiate it when when your object gets hit

function OnControllerColliderHit(hit:ControllerColliderHit){ 
    
   if(hit.gameObject.tag == "findo"){ 
       
      //Instanstiate    your particle prefab here  
      Destroy(hit.gameObject); 
   }    
}

Hope it helps,

Ray

Thanks for replying me!

I have tried your suggestion, but I’m not familiar with using Instantiate at all.
I searched the information about script of prefab and instantiate, trying and modifying many details.
But still dont know the correct way.

well…at first I put the particle into a new prefab (named “prefab_eatfindo”) and typed

Instantiate("prefab_eatfindo");

at the place you suggest.
It seems like the form is totally wrong :sweat_smile:
After that, I also try to add some parameter I’ve found from UnitySupport like:

Instantiate("prefab_eatfindo", Vector3(10,10,10));

Many many embarrassing example like that. I really have no idea.
I think my experience is so poor to solve the problem. What I need is more study although its not my field.

Thanks for the reply again!

Hello,

The FPS tutorial has an explanation of instantiating game objects, rigidbody.

Also there are lots of tuts somewhere in the forum, I think a user posted some of the links. Those tuts should help you get some of the needed info to get you going.

Here’s an example:

//variable for your particle prefab that you created and saved as a prefab.
//drag that particle prefab here in the inspector

var particle : GameObject;


function OnControllerColliderHit(hit:ControllerColliderHit)
{ 
     if(hit.gameObject.tag == "findo")
    { 
       var theClonedParticle : GameObject; // this will create a holder for the particle so we can destroy the instance of the prefab

       theClonedParticle = Instantiate(particle, transform.position, transform.rotation); // this line will instantiate a copy of the prefab to the location of the game object it is attached to.
			
      theClonedParticle.particleEmitter.emit = true;// since it's a particle emitter, we just tell it to emit just look at the paarticle prefab you created if the emit box is uncheck

     Destroy(theClonedParticle, 3);//we destroy the particle in a few seconds 3 seconds for example

     theClonedParticle.audio.Play();//if you want to play some background explosion or something, just make sure you have audio source attached 
     Destroy(hit.gameObject); 
   }    
}

Something like that might work

the tuts from FPS asa well as unity tutorials explains things better than I can.

Hope that helps.

Ray

I have tried the code. I put it in script for character and it works!
Now the particle can correctly emit and destroy at the right time. The comment you posted is very clear.
I am so glad to see it run!
I found FPS tutorial (http://unity3d.com/support/resources/tutorials/fpstutorial ) and downloading now, it’s just ignored as I was not understand what is FPS.

Really THANKS for your kindness!
I will keep researching the scripts.