Search Unity

How to chip away pixels from game object?

Discussion in 'General Discussion' started by bobueland, May 13, 2020.

  1. bobueland

    bobueland

    Joined:
    Sep 27, 2019
    Posts:
    19
    BeforeHit.png DuringHit.png AfterHit.png

    The three images above show the shield before the impact, during the impact and after the impact.

    I am trying to recreate a faithful clone of the Space Invaders game. When the alien bullet hits the red shield a part of it is chipped away. Which part depends on which pixel was hit and of the yellow pattern.

    What is the best way to implement this? The only way I can come up with is to:

    1. Make up the shield of individuals prefab pixels.

    2. After the alien bullet collides with a pixel, take its position and then go through a list of points defined by the yellow pattern. (The yellow pattern is an 6 by 8 matrix where some points are on and some off). Using this list instantiate debris game objects (one pixel each) that then collide with pixels of the shield letting them destroy those pixels.

    My idea seems far fetched and complicated so my question is if there is any better way to implement this.

    BTW if you want to see the space invaders in action here is a link
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    I'd use neither approach.This stuff is trivial when you use software rendering, and becomes more complicated when you use accelerated rendering.

    Given how small the ships are, I'd simply store ship "pixels" in memory as array and upload them to texture. The game could use individual textures per ship, or texture atlas for all of them.

    It means that each ship will be a textured quad with a "procedural" texture, state of the ship would be stored as an array, when it gets hit, the image is updated and hit pixels are subtracted.

    And it would likely be much faster than trying to have individual game objects per pixel.
     
    bobueland likes this.
  3. bobueland

    bobueland

    Joined:
    Sep 27, 2019
    Posts:
    19
    @neginfinity thanks. I found more info by looking up "Texture2D.SetPixel" and "Sprite.Create" in the Unity Docs. Also looked at the following tutorial.
     
  4. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,139
    Do not use SetPixel/SetPixels for this sort of thing. Those operations are notoriously slow.
     
  5. bobueland

    bobueland

    Joined:
    Sep 27, 2019
    Posts:
    19
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
  7. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,139
    I mean, you could do that, but I think it'd be best to use a render texture as a buffer and then apply stamps to the RT. While a more advanced implementation, it's also extremely more performant.
     
  8. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    You won't be able to perform collision detection with it.

    Also with space invader kind of level of detail performance doesn't matter. You could probably implement the whole thing with software rendering, in C#, and still get way over 60 fps.