[Question] Particle System Collision

Hello Guys,

I have been playing around with particle systems and trying to do the same things that I can do with a projectile collision. So, with the Particle Systems, I can detect if there has been a collision with a specific object, but, I cannot figure out “where” a collision occurred.

Like with a projectile you can use code like the following:

     ContactPoint contact = col.contacts[0];
     Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
     Vector3 pos = contact.point;

     Instantiate(bulletDamage, pos, rot);

with this, at the first point of collision you can make a prefab appear showing damage and or make a Particle system play at the point of contact making it look a little better. Now is their any way of replicating this type of thing with a Particle System? You know, making a prefab show some damage where Particle System collides.

If you can help here is a big thankyou and advance!

Cheers.

You want to use OnParticleCollision. See the example here Unity - Scripting API: MonoBehaviour.OnParticleCollision(GameObject)
You query the particle system using GetCollisionEvents which will give you the information you need.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ExampleClass : MonoBehaviour
{
    public ParticleSystem part;
    public List<ParticleCollisionEvent> collisionEvents;

    void Start()
    {
        part = GetComponent<ParticleSystem>();
        collisionEvents = new List<ParticleCollisionEvent>();
    }

    void OnParticleCollision(GameObject other)
    {
        int numCollisionEvents = part.GetCollisionEvents(other, collisionEvents);

        Rigidbody rb = other.GetComponent<Rigidbody>();
        int i = 0;

        while (i < numCollisionEvents)
        {
            if (rb)
            {
                Vector3 pos = collisionEvents[i].intersection; // You want this value!
                Vector3 force = collisionEvents[i].velocity * 10;
                rb.AddForce(force);
            }
            i++;
        }
    }
}

Thankyou for your reply :sunglasses:. I had no idea about GetCollisionEvents, this will allow me to make some headway with the spellcasting in my current project.

Cheers.

edit: It did work - thankyou so much :):sunglasses::smile:

1 Like