- Home /
How to slice a sprite realime C#
Hello there, in my 2D game I want to have my characters dying effect to be being cut in half (random degree 0-359 and then straight across).
I am just wondering how to get this accomplished. I do not care to purchase any assets I would like to do this from scratch. I'm a pretty good programmer in C# and JS but I cannot seem to find any functions in the script reference that would apply.
Answer by Kiloblargh · Dec 12, 2013 at 12:08 AM
I haven't actually used the 2D sprite features yet (I'm still too busy with the 3d game I've been working on since before the 2D stuff was added) but the approach I would take is to use a shader that takes an alpha mask texture, duplicate the object into two copies in the same spot, and apply a half-black-and-half-white mask texture to one, invert the texture and apply it to the other.
If you don't really need it to cut in a totally random direction, you could create as many slice-masks as you like and pick one from an array at random. (This approach would make a torn-looking, anti-aliased edge much easier to achieve.) If you feel you do, you will have to use Texture2d.SetPixels and test whether each pixel it is above or below a line with a random slope passing through the center, using your mighty math powers... I could work out the code to do that if it were my project but don't have the spare time right now to do it for you, so if you're a pretty good programmer I'm sure you can figure it out.
Answer by EX_Darius · Dec 12, 2013 at 12:05 AM
As far as I know you would need to create 2 seporate gameobjects and child them into the empty main character object, then moving them apart if your character dies.
However Ive seen some shaders that are able to do what you described without having to create multiple objects etc.
I suggest you take a look in the shaders part of the asset store.
Thanks for the reply, I will go ahead and take a look at the shaders in the asset store. I actually had no idea it was even possible to have this effect through shaders. Doesn't make a lick of sense to me. Do you think you could give me the name of one? It seems the asset store on mac is a little bit broken when trying to browse.
I think the shader for 3d models was called "Fragmentum" , im not entirely sure if this will work for sprites but its worth a shot.
Answer by junglejapes · Dec 12, 2013 at 12:32 AM
Thanks a lot. I hadn't even thought about doing anything other than a straight up c# only super math power solution. The shader solution works. Thanks a lot guys.