Search Unity

Destructible 2D ☄️ Dynamic Sprite Destruction

Discussion in 'Assets and Asset Store' started by Darkcoder, May 29, 2014.

  1. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Sorry for the late replies everyone, the notification system for this forum makes it too easy to miss. I recommend emailing me if I don't reply here within a day.

    Yes, if you add a 1x line between two areas of the image then you will have made two pixel islands. Clicking "Try Split" will then break it into two. Remember that the calculations are done on each pixel, so the line must actually be a clean cut through the image, 1px may create many connections depending on how you make it.


    This is something I forgot to re-implement in V3. I didn't like the original implementation though, because it lead to scenarios where you could damage a sprite and see no change, but the second or third hit would actually cut through it. I considered making it so the density texture instead acted as a damage threshold, but this might result in strange visuals, it's also unclear how it should function with the new Channels or Paint settings I'll have to play around with in the next version.

    I agree that subtraction alone is insufficient. Replacement may also look bad though, because the alpha channel would have to be used to control the blend strength, then you have less control over the alpha channel change. Ideally there would be a large selection of blend modes to cover each scenario, but combined with the density texture you can quickly end up with many different variants of the main painting code, which results in tons of painting code because for maximum performance I have to unroll these loops. Perhaps some way to create a sequence of individually simple blends is the way to go, I'll have to think about it.


    Most of the internal code isn't designed to be called in game, but most of the public methods and properties are, and they are commented. If you find something that isn't commented enough then let me know so I can improve it.

    The colliders are indeed placed on a child GameObject, so destroying that won't destroy the parent destructible object. This is mainly done because the D2dPolygon/EdgeCollider generates a lot of colliders, and placing them on the root GameObject will make it difficult for you to edit it, and it would also slow the editor down a lot when you view so many components in the inspector. There's also the benefit that the collider can be pixel aligned very easily using the child Transform values, which simplifies the collider generation code.


    I haven't tested D2D with any pathfinding assets. Due to the nature of regularly updating complex colliders it may not be such a good idea to directly integrate them using the colliders though, some kind of custom integration using the pixel data might be better. Not working with multiple colliders sounds like a bizarre limitation though, perhaps that's a new feature they could add ;)


    I'll check it out, I have no idea why this material's shader would change.


    I believe there was some kind of issue where Unity handles FullRect sprites differently, but I'll have to test it out again and let you know. If there is some difference I should definitely mention it.
     
  2. mnar786

    mnar786

    Joined:
    Jan 11, 2017
    Posts:
    9
    hey there.
    i m in trouble with you destructible 2d plugin. i've an image where i've used fixture to fix some part of image. it's work fine when i cut the image without disturb the fixture, but some time fixture stop working and my piece of images doesn't fall.
    in actual all piece of image which is going to fall is child of main image. once i un check the isKinatic option the piece instant fall. take a look video attache
     

    Attached Files:

  3. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    I'm not exactly sure I understand the issue. The D2dFixture component will stick to the solid pixel it's on top of, and if that pixel is destroyed then the fixture will destroy itself. This allows the D2dFixtureGroup component to perform an action (like disable kinematic) when this happens.

    If your fixture is breaking when you damage near the pixel it's on top of, then it's likely that your fixture wasn't on a fully solid pixel, and some change (e.g. splitting) near the fixture caused it to break off. To avoid this you may have to move the fixture to a better location, or increase the opacity of the pixels it's on top of.
     
  4. haim96

    haim96

    Joined:
    May 24, 2013
    Posts:
    107
    Hello,

    I'm using your asset (latest version on unity 2019.2) to create destructible ground.
    the screen is side scroll and I'm using pooling system to optimize instantiating the ground parts.
    (the ground is static, only the camera moves)
    before i instantiate ground part I'm running the rebuild function to clear it from damage.
    on the editor every thing runs fine but I'm getting odd results on android device.
    it seems that rebuild doesn't work correctly and the ground sprites looks distorted.
    the sprite that i use as ground is part of atlas, any chance it effect the rebuild processes on mobile?

    any advice to achieve my goal and keep good performance for mobile devices?

    thanks,

    Haim.
     
  5. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    It's possible the atlas can cause this to break. Does it get fixed if you remove it from the atlas? If so, can you tell me the settings of your atlas? If I can't replicate it then I may need a copy of your setup so I can see where it's going wrong.

    To optimise performance I recommend you use the 'Optimize' setting in the D2dDestructibleSprite component as much as possible. This setting is vital for mobiles, because your visual sprite is often quite high resolution, but the destruction can often be done on a much lower resolution texture and still look good.
     
  6. haim96

    haim96

    Joined:
    May 24, 2013
    Posts:
    107
    thanks for reply,
    i separated the ground from the atlas but i haven't try to compile it for mobile yet.
    i notice that if i optimize the collider ,when i rebuild it in run time it rebuilt in non optimized form.
    i tried to check "rebuild in run time" and set the count to 2 but it doesn't work...
    is there a way to keep the optimization and rebuilt to that form in run time?


    quick update... it seems that separating the sprite from the atlas resolve the problem...
     
    Last edited: Aug 12, 2019
  7. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    If you call Rebuild yourself manually then it will discard any changes made previously. Rebuild has an overload that takes int optimzieCount, so you can always use that. If you set rebuildInGame and rebuildOptimizeCount and remove your manual Rebuild call then it should automatically rebuild as soon as the Start message gets called.

    As for your atlas, what kind of settings does it use?
     
  8. haim96

    haim96

    Joined:
    May 24, 2013
    Posts:
    107
    thanks,
    i will try the Rebuild with optimzieCount option. does it have performance issue that i need to be aware of?
    for the atlas, in fact it's sprite sheet i made in photoshop and split in unity sprite editor.
     
  9. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    optimizeCount works fast, and will speed up your destruction calculations by 4x each time you call it.
     
  10. haim96

    haim96

    Joined:
    May 24, 2013
    Posts:
    107
    well, i found much simpler solution! i added to the script D2dpolygonCollider two more option:
    Square128 = 128,
    Square256 = 256

    that way i create them much more optimized without the need to optimize in run time...
     
    Darkcoder likes this.
  11. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    Hi there! I was just trying to find information on what scale 'cell size' operates on, is it pixels? It doesn't seem to be world units.

    But separately, I saw 'scorch marks' as one of the features on the asset page now, I was wondering how to implement this?

    Thanks!
     
  12. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Yes, it's in pixels. Specially, the pixels of the destruction data, which depend on the amount of times you 'Optimize' the sprite.

    You can find an example of scorch marks in the "26 Scorch Marks" demo scene. This is also used by the Forest/Scorch demo scene. Basically you just need to adjust the Channels setting to have RGB values, and then make sure your stamps/explosions modify the RGB values. To make sure the scorches are visible your RGB channel modifications should cover a larger range than the alpha modifications, otherwise they will just get hidden.
     
  13. ryanspr1vates95

    ryanspr1vates95

    Joined:
    Jul 30, 2018
    Posts:
    5
    Hello,



    I have been having some issues getting an object with the damage destroy script and a collider registering onTriggerEnter2D. I was wondering if you have an example on what I can check? I have tried many combinations to try and get it to work.



    The second question, could you create a callback function from the damage destroy script that lets you assign a function that is called on terrain destruction?



    Thanks,
    NP
     
  14. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    I'll answer you via email now.
     
  15. Litwin

    Litwin

    Joined:
    Aug 15, 2013
    Posts:
    70
    Hi,

    It's possible made like in DDTank, after explode the tile make some "mask" in tile (i mean not only some outline)?

    Example:
    https://imgur.com/YgZPRpR

    Cheers.
     
  16. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    If you're referring to the red color tint applied around the edge of the explosion, then yes, this can be done using the 'scorch marks' feature of Destructible 2D. This can be customized to be any color or texture, just like how the explosion shape can be customized to be any shape.
     
    Litwin likes this.
  17. Lordi1504

    Lordi1504

    Joined:
    Feb 26, 2018
    Posts:
    2
    Hi,
    First of all, sorry for my bad English, i don't have to much opportunity to write in it ;(
    So, my question is about layers example scene in your asset. I was thinking that layers will work in a different way they actually are, what I need to implement is some sort of piercing method that will check if the first layer sprite was penetrate and if it was, damage the second layer sprite.
    As far as i can see right now script just check alpha level and if necessary change bool in sprite underneath, with is not what I need.
    Do you see any way to implement piercing with your asset?

    Thanks in advance for any answer!
     
  18. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Yes, this is how it works now. It would also be possible to read the alpha of the top layer sprite, and then decide which sprite to damage based on if that pixel is destroyed or not. This would allow piercing like you describe perhaps. The only thing that would be difficult to add is performing this for every damaged pixel, so the layer on top acts as a destruction mask on the second.

    I'll see if I can add this new piercing system I describe and send you a build later today!
     
  19. Lordi1504

    Lordi1504

    Joined:
    Feb 26, 2018
    Posts:
    2
    Thanks for quick respond!
    I'm really happy that you see any chance for implementing what I need.
    can't wait for build ;)
     
  20. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Just sent you a new build via private message!
     
  21. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Hey everyone,

    If anyone's using the LWRP lit shader then here's a modified version compatible with Destructible 2D. This was built from the Unity 2019.2.0f1 LWRP 6.91 build.

    I have to do more testing and experimenting before officially including SRP support, since these base shaders are changing quite frequently, not to mention shader graph!
     

    Attached Files:

  22. theunsigned

    theunsigned

    Joined:
    Nov 15, 2018
    Posts:
    33
    Hi, this is a great asset but I'm getting a lot of null reference exceptions when items are destroyed and they seem to be triggered by collision events with other fragments. Any ideas on how to fix this?
     
  23. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Do these errors occur in the example scenes?

    Also, which version of Unity are you using?
     
  24. theunsigned

    theunsigned

    Joined:
    Nov 15, 2018
    Posts:
    33
    I apologize, after looking at it again I realized that it was a simple logic error on my part. Great asset. It really helped me bring my project to a whole new level.

    Thank you.
     
    Darkcoder likes this.
  25. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Hey everyone,

    Version 3.0.4 of Destructible 2D is now out!
    • This new version includes some bug fixes, and also some cool new demo scenes.
    • The new "Cut Opacity" demo scene shows you how to make weaker 'attacks' on your sprites.
    • The new "Paint Multiplier" demo scene shows you how to make weaker or stronger sprites (e.g. metal takes more hits to damage than wood).
    • The new "Slicing Beam" demo scene shows you how to make a lightsaber style weapon!
    • The new "Dynamic Texture" demo scene shows you how to make dynamic sprite textures using a Render Texture!


    Enjoy!
     
    Last edited: Feb 6, 2020
  26. z_yq

    z_yq

    Joined:
    May 3, 2017
    Posts:
    16
    I want to know how to calculate the ratio or area of a picture that has been changed, thank you
     
  27. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    You can access this from the AlphaRatio property, which is on the D2dDestructibleSprite component, you can also see this value in the inspector :)
     
  28. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Hey everyone,

    Version 3.0.5 of Destructible 2D is now out!
    • Improved Slicing Beam demo scene.
    • Added Impact Explosion demo scene.
    • Added Asteroid Laser demo scene.
    • Exposed D2dSlicer.LocalStart setting.
    • Exposed D2dSlicer.LocalEnd setting.
    Here's a picture of the new Asteroid Laser demo scene:


     
    Last edited: Feb 17, 2020
    docsavage likes this.
  29. m-pire

    m-pire

    Joined:
    Jul 12, 2019
    Posts:
    6
    Hi everybody,

    I updated the shader for the lightweight shader pipeline (from the post by darkcoder) to work with the latest universal render pipeline.
    For me it works just fine!
     

    Attached Files:

    Last edited: Feb 9, 2020
    Darkcoder likes this.
  30. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Wow, thanks for updating it!
     
  31. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Hey everyone,

    Version 3.0.6 of Destructible 2D is now out!
    • Improved D2dSplitForce behavior.
    • Improved D2dRetainVelocity behavior.
    • Added D2dSplitForce.ForcePerSolidPixel setting.
    • Added D2dSplitForce.ApplyTo setting.
    • Added D2dClickToSpawn.Interval setting.
    • Added SubtractInvRGB paint mode.
    • Added Explosion Interval demo scene.
    • Added Fracture Click demo scene.
    Enjoy :)
     
    docsavage likes this.
  32. m-pire

    m-pire

    Joined:
    Jul 12, 2019
    Posts:
    6
    I really tried too. After a lot of testing it seems it doesn't work properly though :(

    it's not far off the 2D lights are just kind of "mirrored"
    i recorded a small video:
     
    Last edited: Feb 12, 2020
  33. m-pire

    m-pire

    Joined:
    Jul 12, 2019
    Posts:
    6
    Ok solved that issue :)
    So new version is attached and working so far properly with the URP 2D light!
     

    Attached Files:

  34. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    I just tested it out and yeah it works great :)

    Let me know if you have any other feature ideas!
     
  35. m-pire

    m-pire

    Joined:
    Jul 12, 2019
    Posts:
    6
    Ha ok thats an easy one. Maybe you already got it already and i'm not finding it.

    I'm doing a lot of digging in my game. Horizontally and Vertically. The player can choose the direction.
    I'm using it like in your melting example in combination with a Vector3.MoveTowards(). Sadly it doesn't work 100% reliable.

    To control the animation and that the passive character stops walking I need to know when the character is out of the "digable area" to reset animations, move speed and so on to normal. But your collider changes so i cant use it to test against. Maybe you got some tips to make it work reliable.

    I got a little test level here where I'm trying:

     
  36. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Interesting, I'm not sure I understand the issue exactly though.

    When digging with MoveTowards, you should disable your character controller during this movement, and keep stamping. Once finished, you can enable it again and it should work?

    If you want to avoid scenarios where the character can dig through the sky and 'fly', then you could always do a 2D raycast from the player to some predefined distance down, and if there's nothing there for at least 1 second or something, then you can abort the digging?
     
  37. marcarasanz

    marcarasanz

    Joined:
    Aug 23, 2019
    Posts:
    20
    Hi,

    Am planning to buy your asset, but i wanna know if it could work with my project. Am working on a 2d sidescroller shooter. (Unity 2019.3) Using Universal render pipeline (with normal maps on the sprites) and Corgi Engine. My plan is to use your asset for a gore splatter destruction on my enemies using melee, projectile weapons and explosions. This enemies will have animations. Do you think that this can be achieved with your asset?

    Thank you.
     
  38. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    If I understand correctly, you don't want any destruction features. You only want to apply some kind of blood/gore paint to the sprites? If so, Destructible 2D could do this with a modified URP sprite shader that ignores the destruction alpha and instead only uses the scorch mark RGB data.

    As for animation: If your character is animated using skeletal animation with different sprites for different limbs then this will work, but if it's animated using sprite swapping then the damage/gore won't follow the animation, because it won't know where to map to.
     
  39. marcarasanz

    marcarasanz

    Joined:
    Aug 23, 2019
    Posts:
    20
    Yes, i mean gore and dismembering parts of the sprite, applied to the bullet, some with more or less force. And yep, am using spritesheets for my animations not skeletal. I thought the same.

    I was thinking that maybe i could do one trick, if the enemy reach the point that have no more health, i can do the swap when dies to a sprite of the enemy without animation with a 2d destruction componentes attached to it. This maybe would achieve the effect. What i have in mind is a dismember/death style like the butcher 2d game.
     
    Last edited: Feb 18, 2020
  40. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Yes, stopping the sprite animation, applying/enabling the Destructible 2D components, then fracturing your sprite would indeed be a good solution. This is something I didn't consider, and it would probably make quite a nice demo scene. I'll see if I can implement it in a future version :)
     
  41. Meatloaf4

    Meatloaf4

    Joined:
    Jul 30, 2013
    Posts:
    183
    First off just want to say amazing job on the asset!

    Wanted to point out that it doesn't look like the D2DCalculateMass script is account for the scale in the transform as well.

    Also was curious what's the difference between utilizing this script vs just the auto mass feature in the rigidbody2D?

    Thanks in advance!
     
  42. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    When I originally developed Destructible 2D I don't think this setting existed, so it had to be manually calculated. I guess it is more or less useless now unless your Rigidbody2D has no colliders, I just need to add the ability to set the density so it can work the same.
     
  43. Mr_Mendel

    Mr_Mendel

    Joined:
    Dec 31, 2013
    Posts:
    19
    Hi there,

    I am trying to use Destructable 2D in my project adn have come across an issue. I did look through the forum and it looked like you started to talk about a solution but I could not find any further messages.

    In my game the use dynamically creates a sprite so in the editor the sprite is unknown it could be any shape.

    I tried this to change the sprite of the Destuctable:

    D2D_S = gameObject.GetComponent<D2dDestructibleSprite>();
    SR = gameObject.GetComponent<SpriteRenderer>();

    SR.sprite = GameSpriteManager.Instance.MainSprite;
    D2D_S.Shape = SR.sprite;
    D2D_S.Rebuild();

    And this did infact change teh sprite and it at first all looked well, however, if I attempt a fracture then unity crashes. If I set teh sprite by hand it all works perfectly of course.

    I tried doing:

    public D2dDestructible D2D;
    D2D.RebuildAlphaTex();


    I also tried adding all the components after OnStart execpt the D2D and this didnt work either.

    Thanks in advance.

    Mike
     
  44. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    This looks fine, I'm not sure why it would crash. I'll test this out tomorrow.
     
  45. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    I did some more testing and I found one error that can pop up in a different scenario, but no crashes. I'll send you a private message with some tests.

    [edit] it looks like you have private messages disabled. either enable them or send me an e-mail, thanks.
     
  46. jarbleswestlington_unity

    jarbleswestlington_unity

    Joined:
    Dec 6, 2017
    Posts:
    32
    Hey there, I haven't updated in over a year. On update I had a lot of errors so I took your advice and deleted the root folder and reinstalled from the unity store. Unfortunately this severed all of my script references. Is there a way around this?

    I've been working on this project on and off for a while now, and some of your new updates (particularly the splitting an object while using 2d joints) would be so game-changing for me. I can send you as many details as you would like if that helps
     
    Last edited: May 6, 2020
  47. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    It depends which version you're updating from. Version 3.0.0 was released in January 2019, and was pretty much a complete rewrite from 2.x, so there's no way to migrate without more or less redoing all your destructible objects. To make this easier I recommend you duplicate your project, update one, then look at the settings you had in the old version when setting up the new one. Most of the components and settings are named the same/similar, but notably the destruction itself is different as it now uses the SpriteRenderer to display the destruction, whereas before it converted it to a MeshFilter+MeshCollier. I believe you could still destroy objects with joints in 2.x though (as long as you configure the fixtures properly, you can see the new demo scenes), I just didn't make it a point to mention it.
     
  48. jarbleswestlington_unity

    jarbleswestlington_unity

    Joined:
    Dec 6, 2017
    Posts:
    32
    Aw damn I guess I have my work cut out for me, I'll let you know if I can't find a replacement
     
    Darkcoder likes this.
  49. jrmgx

    jrmgx

    Joined:
    Oct 21, 2016
    Posts:
    41
    Any news on that topic? I'll be a new user if it would work!
     
  50. jarbleswestlington_unity

    jarbleswestlington_unity

    Joined:
    Dec 6, 2017
    Posts:
    32
    So it took a couple days but I've fully switched over to the new version, and wow it's actually amazing how much you improved it. So much of my game relies on complex interactions with 2D destructible and so many of the janky workarounds I had created or was planning to create were totally fixed. This was already such a good plugin, but now it's a masterpiece. You should be damn proud of it.