- Home /
Using sprite mask to shoot irregular shapes out of a cannon without being visible until out of the chamber
My game is implemented in 2.5D (some 3d models, but mostly 2d sprites where we play with a certain amount of camera rotation and z axis to yield our effect. There can be many cannons on the screen shooting projectiles of varying shapes and sizes. A cannon is defined in layers. The ones that pertain to this problem are the screen-facing side of the cannon and the layer representing the inside of the cannon. I want my projectiles to be sandwiched in between as would be expected.
I was able to get something passable working by masking the projectile layer where the projectile shouldn't be visible yet. This looked fine when shooting but there were massive downsides that seemed to only have "non-solutions" when trying to prevent the mask from masking projectiles that aren't being shot by that cannon. (there can be many enemy cannons stacked on each other and your own shots can overlap yourself also)
I know that procedurally making more layers to handle this is wrong, but what can I do?
I also considered growing the projectile overtime on spawn but this is additional complexity and requires well-understood formulas and potentially constrains the design.
Any ideas would be appreciated since this is the largest graphical holdup preventing me from releasing my game.
Answer by minearjade · Jul 05, 2020 at 02:57 AM
If the cannon is a sprite, I would recommend splitting it into two, one indicating the inner sprite area with some underlying bleed to avoid lines, and the other to draw the rest of the sprite. You can then use sorting layers on the sprites to draw them in order, placing the fired object in between. This is actually cheaper than using masking, and much less complex than writing a shader for this behavior. If the situation is more complex (which tends to be the case in 2.5D), I would recommend sorting by distance to the camera, or using a custom sort axis if you need something universally particular (in project settings > graphics), which will allow you to draw both 3D and 2D objects with appropriate layering.
Your answer
Follow this Question
Related Questions
Check if Layer is in Layermask? 5 Answers
LayerMask for RayCast 1 Answer
Compare layers 1 Answer
Two Animations at Once Question 1 Answer
Colliders attached to objects on separate layers are colliding? 1 Answer