Destructible 2D ☄️ Dynamic Sprite Destruction

I don’t think I thanked you for helping me last time, my version said up-to-date in the unity store–and I had just downloaded–but it turned out I WAS one update behind. Good to see you’re still improving this awesome plugin!

I was wondering if you would be able to suggest where to start on interacting with your code slightly. My game has a character lifting (and swinging from) destructible blocks. Short gif for context: DelightfulTestyKob

In the clip you can see that my hand seems to glitch out whenever a bullet collides with a held object. This is actually an improvement from the previous issue where my hand would suddenly not be attached to a real collider, or be attached to a smaller piece of the collider far away from the target point. My solution was to “wait till end of frame” before my hand destroys itself and respawns in the same location. This seems to cause some slowdown and is not entirely accurate. I was wondering if you could tell me where exactly in your code I can trigger an event right after a new object is formed, or if you have a better idea for how to maintain a grip on your objects?

1 Like

It looks like your grapple code depends on a specific Collider2D being unmodified for it to remain connected, which stops working because D2D is updating it whenever a pixel in that particular collider cell is modified. To fix this, I recommend you modify your grapple code to instead depend on the GameObject the collider belongs to, and to store a local position for the grapple point. Your grapple update code can then keep checking to see if it’s still on top of a collider that belongs to the grappled GameObject. This should maintain a lock up until the pixel it’s grappled to gets destroyed and the collider no longer covers it.

Hmm that’s a good idea, I’ll see what I can do

So I tried coming up with an alternate system of manually updating the grapple, but I started realizing how useful accessing a specific collider is for me. Is it possible you could point me in the direction of the moment the new collider is finished? I can create a trigger that checks for a grapple being attached and then re-spawns the grapple physics connection

The colliders are rebuilt from the D2dDestructible.OnAlphaDataModified event, which triggers D2dPolygonCollider.OnAlphaDataModified, which calls D2dPolygonCollider.RebuildCell for each cell.

Hi,

I’ve been trying to figure out why D2dExplosion.Start() is causing me massive performance spikes but can’t seem to find a real cause. The spikes vary in size greatly. The performance hit ranges from perfectly reasonable to freezing the game for seconds at a time on an iPhone. No matter the device (PC, Android, iOS), the performance hit can easily and noticeably reduce the frame-rate. I’ve observed that the issue only occurs if “stamp” is turned on for the D2dExplosion object (Might slightly narrow it down).

Related Profiler Images:

Any ideas? I’m happy to provide more information or screenshots as needed.

Can you post a screenshot of the inspector for the object you’re trying to destroy? On PC you can enable deep profiling so you can narrow it down.

Looks like it’s having a hard time calculating new islands.



Seems pretty slow for a destructible with only 6700 solid pixels. In the next version I will improve collider regeneration speed and allow for pooling which should speed this up a bit.

I’m currently on holiday though so I can’t work on it yet.

hi there,
i hope you are fine. i am again here, sorry to take your important time.
i want to ask to thing.

  1. i have problem with instantiate game object position. (take a look attached image).
  2. how to apply undo operation, on Destructible 2D image?
    thanks in advance?

3599697--292152--error.jpg

1 - You need to modify the CloneForSplit method in D2dDestructible.cs to order the children how you want. For example, you can add this after the first instantiate line: clone.transform.SetSiblingIndex(transform.GetSiblingIndex());

2 - You can call yourSnapshot = yourDestructible.GetSnapshot() on your destructible before you damage it, then call yourDestructible.ReplaceAlphaWith(yourSnapshot); after. This will only undo alpha damage though, if you need to undo more things you can either clone and disable a previous state, or extend the snapshot code to include more data.

Hey there,

I’m having some issues with the polygon collider generation. I’m using the actual points of the polygon for targeting and they are not always there, so I have bits and large chunks of sprite leftover, still showing about 10% remaining alpha but with no collider to actually hit/lock onto.

I noticed when I look at it in the editor(in pause) that not all the collider grid it shows in editor mode is always there in run mode. Is there some way to make it so the colliders are always on/generated?

Colliders are only generated for “alpha tex” pixels that are solid, which means they have over 0.5 opacity (128). Pixels that are just below this are still visible though, which allows for the destroyed object edges to look smooth. If you want to increase the sharpness of this cutoff, then you can increase the “Sharpness” setting in your D2dDestructible inspector.

I’m not sure what you mean by the grid not showing in paused run mode, as long as you select the destructible that has colliders they should show up. Can you upload a screenshot showing the issue?

here they are, sorry for the late reply:
These are the base colliders, it shows them all there here, and I have an asteroid from the example scenes for D2D that generates the same and just works.


After the laser starts damaging the image, only some colliders near the damage are there, and then some random ones near the top right edge.

When it goes past the ‘main’ body, it seems like the colliders don’t generate and there’s nothing for the laser to lock on to.


I looked at it step by step while running, and it looks like the colliders are only generating/showing up when an explosion stamp is near them/hitting them. I’m guessing you might be storing them somewhere but hiding them from the simulation for efficiency, is there any way for me to retrieve them from your scripts?

I’ve gotten this to work over the network but I’m having problems with autosplit not spawning the new objects from the network manager. Is there a solution to this built in somewhere I’m missing or is that bit not compatible with unet out of the box?

Sorry for the late reply, I missed your message. Could you send me the image you’re using via email or private message?

There is no networking support built in. You would probably have to modify the instantiation code in the split method to handle the networking.

I purchased this recently, and am excited to begin using it! I think the click to stamp script is really cool, but I am wondering how I can “instantiate” (in a way) a stamp at a gameObject’s location instead of the mouse’s? Like if I were to shoot a projectile?

The D2dExplosion component can be used to stamp, damage, and add force to nearby objects. If you make a prefab with this component and set it up, then you can instantiate it from your code when you need to make an explosion there. This same approach is used in many of the demo scenes via D2dClickToSpawn, D2dBullet, etc.

Yes, this helped! Appreciate it! Also, is there some sort of collision detection? So like, I have a player, and need to know when he “isGrounded”. But with your script’s edge collider, it doesn’t actually have a collider box. If there is no way to detect it, then what possible workarounds might you suggest?

That’s up to your character controller code. The D2dEdgeCollider component just generates Unity’s built-in EdgeCollider2D, which can be interested with like any other 2D collider (e.g. raycast). To detect the floor you would probably want to detect the normal/direction/angle of the hit surface and make a decision based on that.