- Home /
I instantiated the individual pixels of a sprite as gameObjects, and gave them rigidbody2Ds and BoxCollider2Ds, when instantiating them, they 'explode' away from eachother.
I've been inspired by the following reddit post: https://www.reddit.com/r/Unity3D/comments/4cbw1g/made_a_screensaver_by_shooting_sprites_at_each/
I'm trying to achieve the same thing. So I've created a script that goes over a sprite, and reads each pixel's location and color. The sprite I'm using looks like this: (The sprite is just 10x10 pixels big, hence the low resolution.)
Then, it instantiates a pixel-prefab, which I've made, on each of those locations, and gives it the same color as that pixel on the sprite has. The pixel-prefab is basically a 1x1 pixels white .png file which I've given a rigidbody2D and BoxCollider2D in Unity. I've made it so that it's not affected by gravity.
The problem is, that when I instantiate all these pixel-prefabs, they explode away from each other, as can be seen in the following .gif:
https://i.gyazo.com/5ba2f89708aae9b6e2985dc8eb31463c.gif
When I disable the BoxCollider2D component of the pixel-prefab, the problem ceases to exist. Giving the following result: https://i.gyazo.com/37c87abd2b90bc838c68766e6980d35d.png This is not a solution, since I'm trying to make the sprite, made out of multiple pixel-prefabs, to collide with another sprite, as can be seen in this gif: https://gfycat.com/DimpledFlippantCuckoo
So, basically, I don't want the sprite to fall apart prematurely.
Things I've tried are: 1) Put the pixel-prefabs of the first sprite on one layer, called layer1, and the pixel-prefabs of the second sprite on another layer, called layer2. Then I make Unity ignore collisions between layer1 and layer1, and between layer2 and itself as well, so that the pixels of the same sprite don't collide. This gives following result: https://gyazo.com/2cc52ff4926e564edb5419981c9c1cf3 This is not the result I'm looking for, since the pixels of the first sprite then just awkwardly fly through eachother.
2) Disable the BoxCollider2D of the pixel-prefab, and write a script that enables it when it comes close to the pixel-prefabs of the second sprite. This has as result that even on the slightest touch between the two sprites, the BoxColliders enable and the explosion takes place anyway. That is not a solution to the problem, it only delays the problem.
3) Shrink the BoxCollider2D-component of the pixel-prefab, so that it doesn't collide immediately once instantiated. The explosion still takes place, although I don't understand why this happens. So, still not a solution to the problem.
Anybody has any ideas?
You could simply make the rigidbodys kinematic until you are ready for them to explode.
Or perhaps have them ignore collision with each other.
Your answer
![](https://koobas.hobune.stream/wayback/20220612074107im_/https://answers.unity.com/themes/thub/images/avi.jpg)