Particles triggering a script ?

Hi guys, im trying to get my particles to interact with my player, basically i have created an oil leak with particles and i want my players oil meter to refill a small ammount everytime a particle hits him (like standing underneath the oil leak to recharge) … the script seems to be working fine as i have tested it with various colliders, but i dont understand how to use particles to do it…

I have tried multiple things but my current test has collision and triggers ticked in the particles menu, with my player game object set in triggers - colliders section… i have inside and enter set to callback…

Can anyone help shed some light on how to get the effect that i want, this has been frustrating me since yesterday haha… thank you in advance :slight_smile:

Note: my script is set as an “OnTriggerEnter2d” would it need to be an “OnCollisionEnter2d” instead ?

Physics callbacks are for collider/collider interactions and only the physics system produces physics callbacks, not the particle system. The particle system just uses physics queries such as Physics2D.CircleCast to detect contacts in the same way you might.

There’s lots of tutorials on this but this one is an official Unity one: Recorded Video Session: Controlling Particles via Script - Unity Learn

In short, central to it is this callback: Unity - Scripting API: MonoBehaviour.OnParticleCollision(GameObject)

Thank you :slight_smile: every search i made resulting in people explaining how to create particles or how to “make explosions” etc. I was unable to find something whereby the particles interact with the world in the way that i wanted…

Thank you for sharing a links i will check them out now :slight_smile:

1 Like

I feel your pain. I’m working with OnParticleTrigger and there seem to be no one able to simply get the collider of the particle that entered it. We have a list of Colliders in the Trigger module, but .bounds.Contains(particle.position) doesn’t work for some reason when you try to check if the particle is inside the trigger.

 private void OnParticleTrigger()
    {

        //Get all particles that entered a box collider
        List<ParticleSystem.Particle> enteredParticles = new List<ParticleSystem.Particle>();
        int enterCount = part.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, enteredParticles);

      
        foreach (ParticleSystem.Particle particle in enteredParticles)
        {
            int amount = part.trigger.colliderCount;
            for (int i = 0; i < amount; i++)
            {
                if (part.trigger.GetCollider(i).GetComponent<FireMagic>() != null)
                {
                    if(part.trigger.GetCollider(i).GetComponent<BoxCollider2D>().bounds.Contains(particle.position))
                    part.trigger.GetCollider(i).GetComponent<FireMagic>().AssistantFire();
                }
                if (part.trigger.GetCollider(i).GetComponent<UnlitCandle>() != null)
                {
                    if (part.trigger.GetCollider(i).GetComponent<BoxCollider2D>().bounds.Contains(particle.position))
                        part.trigger.GetCollider(i).GetComponent<UnlitCandle>().transform.GetChild(0).gameObject.SetActive(true);
                }
            }
          
        }
      
    }

Please don’t necro threads, create your own threads. Also, if you add a tag “particles” you’ll likely get a particle dev to reply.

Note: neither the 2D nor 3D physics systems implement the particle system features you’re discussing here so I’ll move this thread to the Scripting forum.

If you look at what “bounds” is then you’ll see it’s an AABB, not the actually geometric shape bounds of the collider. Asking if a point is inside bounds works; expecting an AABB to accurately represent the shape boundary of any collider won’t work.