1
\$\begingroup\$

I am new to Unity and programming in general, however, I wanted to know how to change a sprite's shape. I want to do this while the game is running, so I was thinking about removing a set of pixels given some condition after an event has happened and then adjusting the collider around the object. However, I don't know how to do this. I have searched in YouTube and Google but so far I have not found anything that is useful to me.
My first thoughts were to make the pixels in a sprite's texture transparent given a condition, and I was able to achieve this, but when the time came to translate the texture into the shape of the sprite I failed miserably. Then I thought about using meshes, but I think that meshes might be too hard on the engine and cause some slowdown. I want to make a digging game where I can dig through many different objects and those are my ideas thus far. If you think there are better ways or know of a place where I can learn or, ideally, were willing to give me some coding techniques in C# for achieving the above, I would be very grateful.

\$\endgroup\$
  • \$\begingroup\$ Have you considered using block based digging, in other words when you dig it removes some blocks from the scene (like Terraria or Starbound). \$\endgroup\$ – Matthew Pigram Feb 11 '16 at 1:17
  • \$\begingroup\$ Yes :D, but i'd like it to be smoother, I don't like those blocky aspects, I'd like to make the terrain continuous. :) \$\endgroup\$ – Guacho Perez Feb 11 '16 at 1:21
  • \$\begingroup\$ Do you only ever dig open pits, or can the player tunnel horizontally? \$\endgroup\$ – DMGregory Feb 11 '16 at 1:23
  • \$\begingroup\$ @DMGregory, he can tunnel horizontally, but I think that maybe if there is a script for digging open pits, maybe we could rotate the terrain ninety degrees and do it horizontally (?) I am not sure :P. \$\endgroup\$ – Guacho Perez Feb 11 '16 at 2:51
  • 1
    \$\begingroup\$ Are you sure you wanted to ask about modifying the shape of a sprite not "destructible obstacles/terrain in 2D game"? I think the title is misleading - you dont care how you achieve it, right? \$\endgroup\$ – wondra Feb 11 '16 at 11:36
2
\$\begingroup\$

Then I thought about using meshes, but I think that meshes might be too hard on the engine and cause some slowdown.

Let me check my clock...yes, it is 2016, having 1 mesh shouldn't slow down anything.

What you need here is a tile based terrain (e.g. terraria) with marching squares. Marching squares is an algorithm, wich is made to smooth out 2d tile based terrain.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I researched Marching Squares, a really nice solution, but how do I make the mesh take on the form of an initial terrain image? Would I not need to subdivide the mesh into many different squares? \$\endgroup\$ – Guacho Perez Feb 11 '16 at 22:19
  • \$\begingroup\$ @GuachoPerez Yeah, you do need to have squares. I have found somewhere a nice tutorial about this, I try to find it. \$\endgroup\$ – Bálint Feb 12 '16 at 6:46
  • \$\begingroup\$ Found it unity3d.com/learn/tutorials/projects/… \$\endgroup\$ – Bálint Feb 12 '16 at 6:48
  • \$\begingroup\$ Alright, I implemented the code in the first and second videos but instead of randomly generating the cave, I used an image as a blueprint for the map(copying pixel by pixel into the integer map), it kind of slows down the game by a tiny bit, it is only a 100×100 image, is there a way around this? \$\endgroup\$ – Guacho Perez Feb 12 '16 at 23:37
  • \$\begingroup\$ @GuachoPerez Make sure to have as few I/O as possible. I'm not a unity progranner btw, I only like to go around and give tips for people about different algorithms. \$\endgroup\$ – Bálint Feb 13 '16 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.