Search Unity

Resolved My onParticleCollision() isn't working

Discussion in 'Physics' started by jonathanpecany, Apr 16, 2023.

  1. jonathanpecany

    jonathanpecany

    Joined:
    May 9, 2019
    Posts:
    27
    I have been looking all over the internet for an answer. However, none of them seems to be a solution for me.

    I am trying to get particle collision on an enemy for this game I am working one. The particle system acts like cold mist and when the enemy runs into the particles, they are to slow down. However, onParticleCollision() isn't even being activated.

    Here is my snippet.
    Code (CSharp):
    1. private void OnParticleCollision(GameObject other)
    2.     {
    3.         Debug.Log("Particle collided");
    4.         Debug.Log(other.tag);
    5.         if (other.tag == "ColdAir")
    6.         {
    7.             chilly = true;
    8.         }
    9.     }
    I also got some screenshots of the properties of the inspector menu.

    Now, why isn't it being activated correctly? I have wasted a good 3 hours trying to figure this out.
     

    Attached Files:

  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    Your collider has "IsTrigger" enabled. Triggers don't generate collisions, by definition: their only purpose is to trigger actions when objects go inside them.

    Either use OnParticleTrigger if you want to use triggers, or uncheck "IsTrigger" otherwise.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,478
    Also, you're using a 3D physics collider and have selected the world type as 2D.
     
    Roxanna20 likes this.
  4. jonathanpecany

    jonathanpecany

    Joined:
    May 9, 2019
    Posts:
    27
    That was temporarily until I figured out how to rotate it as I need it facing a specific direction.

    It works now. thx. Although I swear I tried that before with same results. However, with the World 3D set, it doesn't work with me. For some odd reason I can't set the direction, which makes it out of control.
     
  5. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    if your “cold mist” should slow particles down, perhaps use a particle system force field instead and apply drag when inside.
     
  6. jonathanpecany

    jonathanpecany

    Joined:
    May 9, 2019
    Posts:
    27
    That, I wouldn't know how to do yet. Still quite new at particle systems.
     
  7. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    something like:
    * enable external forces module,
    * create new game object that contains a ParticleSystemForceField component,
    * add force field to external forces list,
    * tweak shape/size/drag in the force field, whilst particles are travelling though it
     
  8. jonathanpecany

    jonathanpecany

    Joined:
    May 9, 2019
    Posts:
    27
    Thanks. That helped a lot. So thanks again.
     
    richardkettlewell likes this.
  9. SassyPantsy

    SassyPantsy

    Joined:
    May 17, 2020
    Posts:
    142
    Hi, I'm actually also having trouble getting OnParticleCollision callback.
    My physics world and particle system world mode is both 2D, my colliders aren't marked as isTrigger. It also detects collisions - i.e, moves the particles accordingly when a collider touches them, but the actual OnParticleCollision isn't called.

    I've tried both when the script with the OnParticleCollision methoed sits on the game obejct that has a collider, and when it's on the particle system. In no case do I get the code inside OnParticleCollision to execute (currently just a debug statement). Any help with this?


    **EDIT**

    Well I'll be happy to help you, me.

    In order for this to actually work, aside from what's been said you need to also:
    1. Mark "Send Collision Messages" as true (on the collision tab inside the particle system).
    2. Make sure either your collider-object has a kinematic Rigidbody, or you particle system has "Allow dynamic collisions" set (I think that's what its called). If it deosn't work than it's some other combos, but that's the gist of it.
    Oh and I got it to work when OnParticleCollision was on a script that sits on the colliding object, and not on the PS.

    It would have been better to allow OnParticleTrigger that passed the collider as reference, so the triggered collider could execute code, without causing displacement in the particles, but oh well.
     
    Last edited: May 10, 2023
  10. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Check "Send Collision Messages" in the collision module inspector.
     
    SassyPantsy likes this.
  11. Ralin77

    Ralin77

    Joined:
    Sep 15, 2018
    Posts:
    2
    Hello, is it possible to use OnParticleCollision (with "Send Collision Messages" on) and still have the particles go through the object (object's collider is trigger !) ?
    I want particles to damage the player but not physically interact with it, they should just continue their movement.
    Is this possible?
     
    Peilo77 likes this.
  12. Ralin77

    Ralin77

    Joined:
    Sep 15, 2018
    Posts:
    2
    Adding to my previous post (just above) : Is there a third party 2D particle system asset ?
    The particle system in Unity is 3D and some particles are behind the 2D objects.
    I want my particles to be on the same Z (Or same layer order)

    Edit: I found the Layer Order in the Rendering part of the Particle System, so that's ok now.

    But are there good alternatives for the Unity Particle system, like some 3rd party asset?
     
    Last edited: Oct 13, 2023
  13. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Yes, use the Trigger Module instead of the Collision Module.
     
    Peilo77 likes this.