I’m trying to take damage when my particle system collides with the collider of an enemy, but when I call GetTriggerParticles it always returns zero.
My Particle system (flamethrower) has the following properties:
The collider (FireCollision) of the enemy is a Box Collider 2D and marked as a trigger.
The particle system has the following code attached, OnParticleTrigger is called but the GetTriggerParticles is always returning zero.
void OnParticleTrigger()
{
Debug.Log("FireCollision.OnParticleTrigger");
// particles
List<ParticleSystem.Particle> inside = new List<ParticleSystem.Particle>();
// get
int numInside = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, inside, out var insideData);
// iterate
for (int i = 0; i < numInside; i++)
{
if (insideData.GetColliderCount(i) == 1)
{
var other = insideData.GetCollider(i, 0);
if (other == ps.trigger.GetCollider(0))
{
other.GetComponent<ParatrooperModel>().TakeDamage();
return;
}
}
}
}
What am I missing here?
I got it to work.
I had forgotten to add the triggers to the particle system as described here:
https://forum.unity.com/threads/particle-system-trigger.525793/
So the code for the particle system ended up:
public ParticleSystem ps;
// Use this for initialization
void Start()
{
// The name of our custom tag for the weather particle system triggers
string tag = "FireParticleSystemTrigger";
//We get the Particle System Component from this gameobject first
ParticleSystem ps = GetComponent<ParticleSystem>();
//if we can't find it, we abort with an error message
if (ps == null)
{
Debug.LogError("Could not find a particle system component on the firethrower particle system game object!");
return;
}
//We look for all game objects with the weather collider tag from above
var allColliderGameObjects = GameObject.FindGameObjectsWithTag(tag);
//we create a counter so we can add each collider on its own index later
int index = 0;
//Now we look at each collider game object that we found
foreach (GameObject colliderObject in allColliderGameObjects)
{
//We try to get the collider component from the game object
var coll = colliderObject.GetComponent<Collider2D>();
//If we can't find the collider component, we display an error in the console so we know something is fishy
if (coll == null)
{
Debug.LogError("Could not find a collider component on one of the weather particle system trigger objects!");
}
else
{
//if we have found the component, we can add it to the weather particle system
ps.trigger.SetCollider(index, coll);
index++;
}
}
}
void OnParticleTrigger()
{
List<ParticleSystem.Particle> inside = new List<ParticleSystem.Particle>();
int numInside = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, inside, out var insideData);
for (int i = 0; i < numInside; i++)
{
if (insideData.GetColliderCount(i) == 1)
{
var other = insideData.GetCollider(i, 0);
var paratrooper = other.GetComponentInParent<ParatrooperModel>();
if (paratrooper != null)
{
paratrooper.StartFire();
return;
}
}
}
}
And for every enemy I added a GameObject with the tag “FireParticleSystemTrigger” and a Box Collider 2D (not a trigger).