- Home /
Get surface area of sprite
I'm working on a game in which you shoot at a target, the target being a static sprite. Each gun has a "bullet template" which, upon shooting, gets applied to the sprite as a clipping mask of sorts, making the area hit by the bullets transparent. The part I'm struggling with is how to calculate the surface area of the remaining sprite, for scoring purposes. I hope that was clear enough.
Answer by Bunny83 · Feb 17, 2020 at 01:40 AM
Well, there are several solutions which all depends on various factors. First how precise that value should be. Next how many bullet hits there might be and how often you want to get a result. One of the most flexible and accurate solution is to simply use a List and store the position of each pixel in local or worldspace coordinates in that list. When you apply a bullet hit you simply delete those positions which are covered by your bullet hit.
The same thing might be applied to the actual sprite texture as well if you have a lot bullet hits since constantly drawing maybe hundreds of masks each frame would not be that great for performace. Actually modifying a temporary texture would be better in the long run and scales well. If a high fire frequency is expected a hybrid approach might be better. So use actual mask objects and once you have say 10 of them, apply those directly to the texture and remove the masks afterwards.
Anyways when using a List simply the element count in the list compared to the start amount is the value you're looking for.
Your answer
![](https://koobas.hobune.stream/wayback/20220612223450im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Character Sprite in Corner 2 Answers
Use Sprites as Score 1 Answer
Physics2D.Raycast seems tied to 3D z coordinate? 0 Answers
Strange Rearranging when copying Texture2D 0 Answers
Sprites rendered in the wrong order 1 Answer