- Home /
how to know if an object has been englobed by particles
I am working on a game, in this game you are a 2d particle,the objective is to get to the end of the level, to get there you must kill all the bad guys on the way. To kill you must englobe them with a trail of particles that you leave behind wherever you go and that fade over time. The thing is that i am rather a noob with particles an englobing object with them and i have really no clue to how to do this code. I was thinking of creating a number of 2d edge colliders in between each successive particle and then checking if i have gone through one of them each frame, and if i have i would make my mad guy send an ray through a random location and if it goes through one of those a pair number of times it is then outside of the englobed area otherwise it is inside. That was the idea i had in mind although i think to be extremely complicated. Any advice on how to this would be more than gratified. Thanks.
Answer by robertbu · Aug 22, 2014 at 06:16 PM
The code will depend on what you mean by englobed, and how literally you want to interpret the term. First lets say we only consider a particle as participating in the englobing of an object if they are within some defined distance. Second, we consider an object englobed if there are a defined number of 'participating' particles in front and behind the object.
As a concrete example say our enemy/object was about 1 unit on a side, and we only consider particles participating if they are within 1.5 units of the pivot of the enemy/object. And we need 10 participating particles in front and 10 participating particles behind the enemy/object to consider it englobed.
The code that need to do the englobing calculation needs to get access to the particle system. If this is a problem for you, search out answers on accessing components on other game objects.
Each frame from the particle system, you will get the particles. Carefully search out how to do this since it is not like the other places in Unity where you get an array. You have to first create an array the size of MaxParticles, then you need to pass that array in to GetParticles() and include the size. Also, assuming you are using the ParticleSystem, the type will be ParticleSystem.Particle[].
For each particle in the array, you first check the distance between them to see if it is participating. Since this is 2D, you can use Vector2.Distance(), but since the ParticleSystem.Particle.position is a 3D as is the position of the enemy/object, you'll have to cast the parameters to Vector2s.
For all particles that are within the specified distance, you'll check the 'x' position relative to the 'x' position of the enemy/object. If it is less, then you increment a variable for one side, if it is more you increment a variable for the other.
If at any time the two variables (one representing each side of the object) are both over the threshold value, then the enemy/object is englobed.
First of all thank you Robertbu for your answer, by englobe i mean form a circle around the enemy, in such a way that it is surrounded by the particles(by the way i forgot to say that the particles are not moving). Judging by the way you would do this code do you think it would be far to hard to do the surrounded enemy check?
I think i might have an idea, what if each enemy had a set of children, and each one had a box collider attached to it. these children would themselves be placed around the enemy in such a way that when you go inside one of those colliders they would send a message to the bad guy that you have entered. if all the children send this message in a given time then we would consider the enemy surrounded. If one would want more detailed surround effect they would only need to increment the number of children.
Colliders don't receive collisions from particles. While I don't know for sure, I don't believe particles interact with 2D colliders. One post mentioned that it was planned for some future release. For particle collisions, you have to listen for OnParticleCollision() callback on a script on the same game object as the ParticleSystem. Then you have to retrieve a list of the collisions. Given the 3D requirement, I don't believe listening to collisions will work for you.
You could use angle to check 8 separate positions. Have a boolean array of size 8 called 'directions' where all the values start at false. For each particle you can do:
var dir = particle.position - object.position;
var angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
var i : int = $$anonymous$$athf.RoundToInt(angle / 45.0);
var i = i % 8;
directions[i] = true;
If after processing all the particles, all the values in 'directions' are true, then the object is surrounded.
The problem with a 3D collision is they only occur on a surface and only on the front of that surface. Say you placed a sphere or a box at each of the eight points. If the particle was inside the box, it would not be detected. And again, it won't be the 3D object that detects the collision, it will be the ParticleSystem using OnParticleCollision()>
Your answer
![](https://koobas.hobune.stream/wayback/20220613161250im_/https://answers.unity.com/themes/thub/images/avi.jpg)