- Home /
Collision of particles with the box collider 2d does not work if the box collider 2d with the trigger checkbox marked
void OnParticleCollision(GameObject other)
{
if (other.tag == "Enemy")
{
Debug.Log("Text");
}
}
Answer by Chetzy · Sep 04, 2018 at 05:19 PM
The answer is simple, one collider that is trigger never get collisions or physics its only for events, you need to unselect the trigger option and the particle system is going to collision with the collider
Answer by celinedrules · Aug 31, 2020 at 01:11 AM
know this is a little old but I did figure out a really simple way to do this.
In Unity on the particle system just make sure the Triggers section is checked and attach this script to the same GameObject
using UnityEngine;
public class Test : MonoBehaviour
{
// The particle System
private ParticleSystem sys;
// Array of alive particles
private ParticleSystem.Particle[] aliveParticles;
// The 2d collider that we want to collide with
// the particle system
private BoxCollider2D boxCollider;
// Called when any particle meets the condition
// of the trigger section of the particle system
private void OnParticleTrigger()
{
// Check to see what needs to be initialized
InitializeIfNeeded();
// Get the particles that are currently alive
// and store them in the array
sys.GetParticles(aliveParticles);
// Loop through the particles in the array
foreach (ParticleSystem.Particle particle in aliveParticles)
{
// Check for collision
if (boxCollider.bounds.Intersects(new Bounds(particle.position, particle.GetCurrentSize3D(sys))))
{
Debug.Log("Success");
}
}
}
// Initialize the variables only once
private void InitializeIfNeeded()
{
// Get the 2d trigger collider from the GameObject
// This can be done in several different ways, I chose
// to add a tag and find it that way
if (boxCollider == null)
boxCollider = GameObject.FindGameObjectWithTag("HurtBox").GetComponent<BoxCollider2D>();
// Get the particle system we want to collide with and
// add the 2d trigger collider to it
if (sys == null)
{
sys = GetComponent<ParticleSystem>();
sys.trigger.SetCollider(0, boxCollider);
}
// Initialize the array that holds the alive particles
if (aliveParticles == null)
aliveParticles = new ParticleSystem.Particle [sys.main.maxParticles];
}
}
Your answer
Follow this Question
Related Questions
Particle Collision / Trigger not being reported to On_xx Event 1 Answer
OnParticleCollision Question (Image included) Particle Decal Hanging off Edge 1 Answer
2D box colliders not touching but are colliding, how to fix? 0 Answers
can we attach a collider for each particle emitted by a emitter 1 Answer
Whirlwind spell collision detection 1 Answer