- Home /
Getting the position/intersection of collided particles
The purpose of this code is to create a fire explosion effect at the intersection of a particle when it collides with a plane. The only solutions I could find to gathering the intersection point was by using GetCollisionEvents and ParticleCollisionEvent. Although, when a ParticleCollisionEvent is added to _collisionEvents the function GetCollisionEvents seems to set the collider using an obsolete method. Is there anyway around this or any other solutions in gathering a particles point of intersection when a collision occurs? I have also looked at using ParticlePhysicsExtensions.GetCollisionEvents, but this is also obsolete.
public GameObject _ExplosionEffect;
public ParticleSystem _FireEmbers;
private List<ParticleCollisionEvent> _collisionEvents;
private void Start()
{
_collisionEvents = new List<ParticleCollisionEvent>();
}
private void OnParticleCollision(GameObject other)
{
_FireEmbers.GetCollisionEvents(other, _collisionEvents);
CreateExplosion(_collisionEvents[0]);
}
private void CreateExplosion(ParticleCollisionEvent particle)
{
_collisionEvents.Clear();
GameObject effect = Instantiate(_ExplosionEffect, particle.colliderComponent.transform);
effect.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
effect.AddComponent<DestroyEffect>();
}
System.InvalidOperationException: collider property is deprecated. Use colliderComponent instead, which supports Collider and Collider2D components
Answer by bunnynsnake · Apr 13, 2018 at 09:02 PM
This should work...
using UnityEngine;
using System.Collections;
public class OnParticleCollisionAlt : MonoBehaviour {
public GameObject myExplosion;
public GameObject prefabExplosion;
private ParticleSystem.CollisionEvent[] collisionEvents = new ParticleSystem.CollisionEvent[16];
void OnParticleCollision(GameObject other) {
int safeLength = particleSystem.safeCollisionEventSize;
if (collisionEvents.Length < safeLength)
collisionEvents = new ParticleSystem.CollisionEvent[safeLength];
int numCollisionEvents = particleSystem.GetCollisionEvents(other, collisionEvents);
int i = 0;
while (i < numCollisionEvents) {
Vector3 collisionHitLoc = collisionEvents[i].intersection;
myExplosion = Instantiate (prefabExplosion, collisionHitLoc, Quaternion.identity) as GameObject;
i++;
}
}
The stored event still contains the error System.InvalidOperationException: collider property is deprecated. Use colliderComponent ins$$anonymous$$d, which supports Collider and Collider2D components. (The one from the screenshot). As there is this error, all intersections are stored at 0, 0, 0 , so all effects instantiate here.
Also: ParticleSystem.CollisionEvent is obsolete particleSystem.safeCollisionEventSize is obsolete ParticleSystem.CollisionEvent[] collisionEvents = new ParticleSystem.CollisionEvent[16] using an array is obsolete (recommends a list)
Note: In $$anonymous$$any Cases, you can get away without using a loop in particle collision.
$$anonymous$$ake sure you are placing OnParticleCollision()
inside a script that is ON the particle collider object. This is key. Because the other is the object the particles collide with, and it temporarily has a particle system added to it on Collision.
The particle system "part" in part.GetCollisionEvents(other, collisionEvents);
is the system that created the particles. Other is the gameObject
on which those particles died. They are mutually exclusive.
When you separate them you will find it all works.
Your answer
Follow this Question
Related Questions
Why can't I add non-prefab colliders to a particle effects triggers list? 0 Answers
How to destroy object when it collides particle 1 Answer
Fire Collision detection 1 Answer
can we attach a collider for each particle emitted by a emitter 1 Answer
Let Particles sit on Collider without triggering callback - OnParticleCollisionEnter? . 1 Answer