Emit when tag collision

Hello UNITY COMMUNITY, Cobalt60 here with a desperate question. I am trying to figure out a way to have a particle emitter only emmit when the tag “Enemy” is inside the Box collider which is of course on trigger. the thing is the enemy may perish in the collider so I need a way for the emitter to well… Stop emitting… when the enemy has left physically or spiritually hehehehe. Any answers will greatly be appreciated and thank you guys for looking at my post! ;D

Put this on your collider object…

public PartileEmitter particleEmit;

void OnTriggerEnter(Collider col){

if(col.gameObject.tag == "Enemy"){

particleEmit.emission = true;

}else{

particleEmit.emission = false;

}

}

Just drag your particle emitter to the public variable in Inspector

public ParticleSystem particleSystem;
private GameObject enemy;

void OnCollisionEnter(Collision c)
{
     if(c.gameObject.tag == "Enemy")
     {
         particleSystem.Play();
         enemy = c.gameObject;
     }
}

void Update()
{
     if(particleSystem.isPlaying && !enemy)
     {
          particleSystem.Stop();
     }

}

put this on the the gameobject with the trigger collider

Okay. As I did not see in your question that you wanted the answer to be in C#, I’ll be giving you a quick translation in JavaScript.

var PE : ParticleEmitter;

function Start(){

    PE.emit = false;//So that it does not emit at the start, that saves you time in the inspector

}

function OnTriggerEnter(c : Collision){

    if(c.transform.tag == "Enemy"){

        PE.emit = true;

    }
}

function OnTriggerExit(c : Collision){

    if(c.transform.tag == "Enemy"){

        PE.emit = false;
   
    }
}