- Home /
Finding Particle Information Upon Collision
I have just a short question, it doesn't seem to be addressed anywhere so I'm not entirely sure if it's possible, here's my problem:
I have a system set up where particles will collide with a GameObject and deal damage to it. The particles in question shrink as they float through the air. I'm trying to figure out how to find out what the size of the particle is at the time of collision. For reference, it is a constant beam effect, so I can't simply take the starting size. I would like for the player to have to get closer to the enemy GameObject to deal more damage, since the particles would be larger upon collision. (There are likely other ways to achieve this effect, but finding the size of the particle at the time would be the cleanest way to do it.)
TL;DR: How do I find out information about a particle upon collision with a GameObject? (ie: the current size, not the startSize)
Thanks in advance.
I have a system set up where particles will collide with a GameObject and deal damage to it. The particles in question shrink as they float through the air. I'm trying to figure out how to find out what the size of the particle is at the time of collision. For reference, it is a constant beam effect, so I can't simply take the starting size.
When you say 'particle' I presume you are not referring to the Unity particle system, but some kind of system you have set up yourself?
In that case, if you are tracking the size of your particles, can you not simply read out the size of the particle on collision with the GameObject?
I am in fact using the shuriken particle system built in to unity. I have colliders set up on the particles, however I can't figure out how to find information about the particles the way I could with two physics objects.
Answer by richardkettlewell · Jan 09, 2018 at 12:30 PM
Unfortunately you only have access to a limited amount of data inside the OnParticleCollision callback. You can see all the available data here: https://docs.unity3d.com/ScriptReference/ParticleCollisionEvent.html
(and a working example here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleCollision.html)
There is no way to find out the size of the colliding particles using this mechanism, but it sounds like you have some other way of knowing the start size? If that's the case, you presumably have access to a Particle (https://docs.unity3d.com/ScriptReference/ParticleSystem.Particle.html), so you can use the GetCurrentSize method on that structure.
If that's not actually true, then all I can tell you is that it's on our roadmap to improve this API, but currently it is indeed quite limited.
I see. Thanks for the input, I'll have to figure out some other way then. I would absolutely love to see these limitations removed in the future, though.
This was 3 years ago, and the API remains unchanged. You mentioned it was on the roadmap, but I fear this may have been put on the wayside for features such as DOTS. Guess I won't be using the particle system for what I want afterall.
Answer by Josiah_Kunz · Aug 03, 2021 at 02:52 PM
This is an old post, but it still comes up when you Google things like "get particle collision information". I needed a solution for particles as damage sources hitting an enemy or portal:
Make sure the collisions module is checked on your
ParticleSystem
.Make sure
Send Collision Messages
is also checked on the collisions module.On your target (e.g., enemy), make sure you have a
Rigidbody
and either a collider on the target or composite collider children. Colliders can be confusing to people, but here's how mine is set up:
4. On your target, place the following script:
public Rigidbody rigidbody;
void OnParticleCollision(GameObject other) {
// Get particles
ParticleSystem particleSystem = other.GetComponent<ParticleSystem> ();
if (particleSystem == null)
return;
// Here's the meat of the problem
ParticleSystem.Particle[] allParticles = new ParticleSystem.Particle[particleSystem.particleCount];
particleSystem.GetParticles(allParticles);
Bounds particleBounds;
for(int i=0; i<allParticles.Length; i++)
foreach(Collider collider in rigidbody.GetComponentsInChildren<Collider>()){
if (collider.isTrigger)
continue;
particleBounds = new Bounds(allParticles[i].position,
allParticles[i].GetCurrentSize3D(particles));
if (collider.bounds.Intersects(particleBounds)){
// Manipulate particles here! I just kill them for visual confirmation
allParticles[i].remainingLifetime = -1;
break;
}
}
}
particles.SetParticles(allParticles);
}
This lets you know if your particles are within the collider (which is what caused OnParticleCollision
to be called in the first place).
Edit 1: typos
Edit 2: Switched answer from a me-specific solution to a general solution