Search Unity

Question Implementing Pixel-Based Destruction in Unity: Exploring CustomCollider2D

Discussion in '2D' started by Manjunku, May 6, 2023.

  1. Manjunku

    Manjunku

    Joined:
    Jun 13, 2021
    Posts:
    8
    Hello, I would like to implement a destruction system in my game based on pixel color, inspired by Worms or Noita. I noticed that the alpha value of my texture defines the shape of the PolygonCollider2D.

    So far, I've tried modifying my texture using texture.SetPixelData (strugled a lot with Color float type before knowing about NativeArrays) and regenerating the collider by destroying and recreating it. However, I'm experiencing a little drop in FPS when regenerating the collider and splitting the sprite, which prevents me from scaling with this approach.

    I'm wondering if the CustomCollider2D could meet my needs. I've seen that it can apparently be modified without destroying it, but I can't find any implementation examples and I'm struggling to understand how I could use it. I'd like to avoid going down the wrong path if possible.

    Ideally, I would like to be able to separate/cut sprites into pieces based on transparency. If anyone could shed some light on this, no matter how small, it would be greatly appreciated. Thank you!
     
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    i dont think unity is the correct engine to do a game like this, you can do it yes, but if you want to use the unity colliders and setpixels its gonna be super laggy no matter what you do
     
    Manjunku likes this.
  3. Manjunku

    Manjunku

    Joined:
    Jun 13, 2021
    Posts:
    8
    Bruh, yeah with setPixel and setPixels the game is running at 20fps. With SetPixels32 it's arround 120 FPS, with SetPixelData i can update even a large texture every frames and the game is still above 600 fps.
    My problem here is not the texture anymore but the collider refreshing.
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    yeah the refresh of the collider has horrible performance, the bigger your explosion the worse its gonna be. You said that you wanted to scale to bigger and bigger destruction, even the setpixeldata will lag you, the collider regeneration will lag you to another dimension. You can avoid this by implementing your own collision system and terrain like noita, but at that point you arent using any out of the box unity features.

    Obviously if you want only to do something like worms armageddon its gonna be much easier than noita however i still dont believe unity is tooled for that.

    Anyway I wonder if you have watched this video where they explore how noita was made:
     
    Manjunku likes this.
  5. Manjunku

    Manjunku

    Joined:
    Jun 13, 2021
    Posts:
    8
    Yeah i watched this and you're right, i don't wanna fight against unity but work with it.
    So maybe i could change my approach and instead of thinking about pixel destruction i could use some basic shapes and joints instead, with the CustomCollider2D :



    Maybe this, directly treating the shape group or/and with a marching square algorithm to build shapes both for sprites and collider ? (To make random terrain for example)

    The main goal after all is just to have the ability to break something into multiple pieces dynamicly and to have the ability to treat hundreds of sprites (not thousands). The game Reassembly could be a good example too :


    Ships are made of shapes, linked together with joints. Planet and terrain corpses seems to be tilemap based.

    I'm still trying some behaviors on the texture right now.
     
    Last edited: May 6, 2023