- Home /
how to use collision with particle system in 2d?
I want to use collision with particle system in 2D. but it didn't work out. I think that because there is no OnParticleCollision2D callback. Of course it works in 3D.
any one ever think about this? If so please please help me!!!
Answer by Genei_180 · Jun 03, 2017 at 12:51 PM
This is not true anymore
You can use Particel System that Collide with 2D Sprites For that you need to go in the Particel tap to Collision and there Change the Collision Mode from 3D to 2D
I Hope i Could help Someone Have a Nice Day and keep Coding :)
I did this and for some reason it still doesn't work. Ideas?
Answer by HappyMoo · Jan 01, 2014 at 02:26 PM
Particles don't collide with 2D Colliders. If you think about it, it would be hard to decide what should happen anyway, as 2D Colliders live on a constant z-plane and particles move in 3D, so they would rarely hit the exact edge of a 2D collider and more likely move in from or behind the collider.
If you really need this, you can still create BoxColliders etc. in a 2D Game.
Answer by celinedrules · Aug 31, 2020 at 01:09 AM
I 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 System with multple collision behaviors for different layers? 0 Answers
Overlaping particle collision only registering as one collision 0 Answers
Object jumps through roof when holding "Up" button? 1 Answer
How do I make animation transitions a one-way street? 1 Answer
Limiting Physics to X and Y 5 Answers