Search Unity

OnParticleCollision not calling for GameObject with Rigidbody

Discussion in 'Scripting' started by BadSeedProductions, Aug 20, 2019.

  1. BadSeedProductions

    BadSeedProductions

    Joined:
    Dec 26, 2014
    Posts:
    144
    I'm using the basic code directly from the manual https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleCollision.html and it works as long as I keep RigidBody component off of the GameObject (object). I can't find any documentation that states that OnParticleCollision does not work with RigidBody, and this example is clearly set up to work with RigidBody not quite sure what could be happening, as soon as I add a RigidBody component, OnParticleCollision stops being called.

    Code (CSharp):
    1. public class ExampleClass : MonoBehaviour
    2. {
    3.     public ParticleSystem part;
    4.     public List<ParticleCollisionEvent> collisionEvents;
    5.  
    6.     void Start()
    7.     {
    8.         part = GetComponent<ParticleSystem>();
    9.         collisionEvents = new List<ParticleCollisionEvent>();
    10.     }
    11.  
    12.     void OnParticleCollision(GameObject other)
    13.     {
    14.         int numCollisionEvents = part.GetCollisionEvents(other, collisionEvents);
    15.  
    16.         Rigidbody rb = other.GetComponent<Rigidbody>();
    17.         int i = 0;
    18.  
    19.         while (i < numCollisionEvents)
    20.         {
    21.             if (rb)
    22.             {
    23.                 Vector3 pos = collisionEvents[i].intersection;
    24.                 Vector3 force = collisionEvents[i].velocity * 10;
    25.                 rb.AddForce(force);
    26.             }
    27.             i++;
    28.         }
    29.     }
    30. }
     
  2. BadSeedProductions

    BadSeedProductions

    Joined:
    Dec 26, 2014
    Posts:
    144
    I changed collision quality to high, and it now functions. (using Unity 2018.3.0f2 atm)
     
    TheHTTPs likes this.