Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Particles triggering a script ?

Discussion in 'Scripting' started by Charomentis, Sep 23, 2019.

  1. Charomentis

    Charomentis

    Joined:
    Oct 1, 2018
    Posts:
    6
    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 :)
     
  2. Charomentis

    Charomentis

    Joined:
    Oct 1, 2018
    Posts:
    6
    Note: my script is set as an "OnTriggerEnter2d" would it need to be an "OnCollisionEnter2d" instead ?
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    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: https://learn.unity.com/tutorial/recorded-video-session-controlling-particles-via-script

    In short, central to it is this callback: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleCollision.html
     
    Last edited: Sep 23, 2019
  4. Charomentis

    Charomentis

    Joined:
    Oct 1, 2018
    Posts:
    6
    Thank you :) 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 :)
     
    MelvMay likes this.
  5. rafaelbarretobra

    rafaelbarretobra

    Joined:
    Aug 5, 2015
    Posts:
    11
    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.
     
  6. rafaelbarretobra

    rafaelbarretobra

    Joined:
    Aug 5, 2015
    Posts:
    11
    Code (CSharp):
    1.  private void OnParticleTrigger()
    2.     {
    3.  
    4.         //Get all particles that entered a box collider
    5.         List<ParticleSystem.Particle> enteredParticles = new List<ParticleSystem.Particle>();
    6.         int enterCount = part.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, enteredParticles);
    7.  
    8.      
    9.         foreach (ParticleSystem.Particle particle in enteredParticles)
    10.         {
    11.             int amount = part.trigger.colliderCount;
    12.             for (int i = 0; i < amount; i++)
    13.             {
    14.                 if (part.trigger.GetCollider(i).GetComponent<FireMagic>() != null)
    15.                 {
    16.                     if(part.trigger.GetCollider(i).GetComponent<BoxCollider2D>().bounds.Contains(particle.position))
    17.                     part.trigger.GetCollider(i).GetComponent<FireMagic>().AssistantFire();
    18.                 }
    19.                 if (part.trigger.GetCollider(i).GetComponent<UnlitCandle>() != null)
    20.                 {
    21.                     if (part.trigger.GetCollider(i).GetComponent<BoxCollider2D>().bounds.Contains(particle.position))
    22.                         part.trigger.GetCollider(i).GetComponent<UnlitCandle>().transform.GetChild(0).gameObject.SetActive(true);
    23.                 }
    24.             }
    25.          
    26.         }
    27.      
    28.     }
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    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.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    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.