So i’ve got a neat little spaceship sprite with its animated engine thruster happily wiggling through space. Whenever a spaceship gets hit it blinks as a way of confirming that it has been shot. But the thruster also blinks. And of course, it should blink, it is a part of the sprite after all. I can achieve the wanted effect (ship blinks when hit, but thruster remains unchanged) by instantiating only the thruster animation as a prefab whenever i instantiate the spaceship, but it’s quite tedious to do that for a lot of models since i need to nail its exact position in pixels.
The way I would handle this would be to make them separate objects, and give the “Ship” an attachment point as an empty child GameObject.
I would set up all my various thruster prefabs with a parent GameObject representing where they should get attached, and the thruster child object’s local position would be the correct offset for that thruster. This may be no offset at all, or for bigger thrusters it may have to be moved a bit relative to the parent.
Then when you spawn a thruster for a ship, you can set the thruster’s root object position to the attachment point, and it should line up correctly for any thruster. This does require some setup beforehand in the editor, but you’re leveraging prefabs, your code will be simpler, and you only have to set them up once per asset and it will work for any ship.
This method could be used for gun attachments, or any other additional object to the ship.
That is kind of the way i’m working now, but there is a slight problem with that. Unity doesn’t support nested prefabs, and my ships are already nested in an empty object in a squadron. So i’m getting a position of the ship and then locally instantiating and setting gunpoints and thrusters, but it’s a solution which requires me to hit play and adjust the instantiation position for every gunpoint and thruster. It needs to be quite precise since pixel graphic is in question. Since there’s a lot of ships, its quite the work. I’m wondering if there may be another way to store ship flying patterns or instantiate the in a certain flying pattern.
You could potentially use Scriptable Objects to store different data sets to use. The problem of configuration is still there though. What I was suggesting was more along the lines of — drag out a ship prefab, drag out a thruster prefab, align the thruster properly when the thruster root object is aligned with or a child of the attachment point of the ship, then apply the changes to the thruster prefab. You shouldn’t have to go into play mode for that.
I can only think of:
1- get/set pixels
2- shader
3- sepearte game objects
1 & 2 seem to be discussed in the thread you linked, but it’s really not that complicated. I think 2 approaches could work, either a mask of which pixels get recolored or an array of colors that gets recolored (if they are mutually exclusive from the thrust / gun)
For 3 you could also simply have one separate GameObject of the ship (edit: without the thrust animation) that you in case of a hit just position exactly on top of the ship (on a higher layer) and hide it again after the blink
I decided to take a bit simpler approach (at least for me). For spawning enemies in a formation, i create an empty spawner object which instantiates and sets the position of all the objects that are spawned in appropriate formation. For every formation, there’s a different spawner object. That way i can use that one nesting level that we have at disposal for nicely positioning gunpoints, jets, and everything else i might need and doesn’t need coloration. Masking and using arrays seems a bit too much for this, since it’s a simple 2D shooter. Hope it won’t get me in trouble further in development.