Unity Destructible Sprite

by thgil

Simple destructible unity sprites

( Crawled 25 minutes ago )

Destructible sprites with polygon colliders


Attach DestructibleSprite.cs as a script component to the sprite.

When a projectile hits the sprites polygon collider call ApplyDamage(pos, radius) at the hit point.

colliders[i].GetComponent<DestructibleSprite>().ApplyDamage(explosionPos, explosionRadius)

Generating a polygon collider for a sprite at runtime

We start by generating a binary image (b) from the texture.

We can use two helpful functions on this binary image to get a nice clean outline.

Erosion (E) - shrinking of the image

Dilation (D) - boarding of image

We can clean up our binary image by appply these functions. b0= D(E(b))

We can get the outline of the image bs = b0 - E(b0)

Subtraction (S)

We can add each pixel now as a vertex to the polygon collider. Performing some simplification we get a simple path the collider can use.

Each point is a vertex on the collider.


BSD license