- Home /
Question by
N0kta · Mar 10 at 10:45 PM ·
collisionparticlesparticlesystemparticle collision
Let Particles sit on Collider without triggering callback - OnParticleCollisionEnter? .
Is there a way to Disable Collisions for Particles that have already been collided or keep track of them in the code?
I need each particle to trigger the OnParticleCollision once but they do it continuously while standing on the collider.
Something like OnParticleCollisionEnter.
Killing the particle on collision wouldn't work for me as I want them to slowly fade away with size over lifetime while sitting on top of the collider.
Here is an example: This is the visual effect I prefer:
And this is the functionality I need:
parcolfunc.gif
(389.7 kB)
parcolvis-1.gif
(409.3 kB)
Comment
Answer by Master109 · Mar 20 at 06:39 PM
using UnityEngine;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
public ParticleSystem particleSystem;
List<Vector3> intersections = new List<Vector3>();
void OnParticleCollision (GameObject other)
{
List<ParticleCollisionEvent> collisions = new List<ParticleCollisionEvent>();
int collisionCount = particleSystem.GetCollisionEvents(other, collisions);
for (int i = 0; i < collisionCount; i ++)
{
ParticleCollisionEvent collision = collisions[i];
Vector3 intersection = collision.intersection;
if (!intersections.Contains(intersection))
{
intersections.Add(intersection);
// Add one to the number in Collisions text here
}
}
}
}
If you are worried that a particle might fall to the exact same position as another, then use:
using UnityEngine;
using System.Collections.Generic;
public class Example2 : MonoBehaviour
{
public ParticleSystem particleSystem;
List<Vector3> intersections = new List<Vector3>();
void OnParticleCollision (GameObject other)
{
List<ParticleCollisionEvent> collisions = new List<ParticleCollisionEvent>();
int collisionCount = particleSystem.GetCollisionEvents(other, collisions);
List<Vector3> endedIntersections = new List<Vector3>(intersections);
for (int i = 0; i < collisionCount; i ++)
{
ParticleCollisionEvent collision = collisions[i];
Vector3 intersection = collision.intersection;
if (!intersections.Contains(intersection))
{
intersections.Add(intersection);
// Add one to the number in Collisions text here
}
else
endedIntersections.Remove(intersection);
}
for (int i = 0; i < endedIntersections.Count; i ++)
{
Vector3 intersection = endedIntersections[i];
intersections.Remove(intersection);
}
}
}