Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by realtomshar · Jul 24, 2016 at 10:10 PM · 2dterraingenerationdestructible

Creating 'worms'-like terrain

Hi all,

I am fairly new to Unity (only made 2 2d games) and for my next project I want to try and create a terrain generation script to make worms-styled terrain.

My first thoughts was having a 2d array of x and y coordinates with either a 1 or 0 for solid or empty and them somehow using that to render some type of texture with a collider around it. Also, this image doesn't show it but the terrain is destructible too.

My question is, is my idea for a 2d array of x and y coordinates with 1's and 0's a good idea and how could I display that in a textured way like in the image. Or, what is a better approach?

Comment
Add comment · Show 11
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Jul 24, 2016 at 10:25 PM 0
Share

A two-dimensional array would work well for a tiled terrain, but Worms has basically terrain that can be generated and deformed on the pixel level. A better way to do it would probably be to work out shapes by conneceting points that describe the outline, and make a mesh out of it so the inside polygons show up textured.

avatar image realtomshar Cherno · Jul 25, 2016 at 05:33 AM 0
Share

@cherno I was talking about the array of 1's and 0's being for each pixel (kinda like a bitmap i guess). If i dont do it at this level of detail i would be unable to modify the terrain at the pixel perfect level as you suggested? Also, how would I be able to map a mesh to a pixel perfect terrain.

avatar image Cherno realtomshar · Jul 25, 2016 at 09:44 AM 0
Share

I see. Using the array like this would work, I guess. I don't know how big the level could be, and there's also the issue of collision which would have to use a custom physics system because Unity's won't be able to handle it.

To map terrain to pixel perfect terrain, I'd imagine that the whole map is covered by large bitmap showing the terrain, and this texture is modified before the game starts so the areas where no terrain is have their pixel colors set to alpha 0, and the same goes for terrain that gets destroyed during runtime.

I'm sure you will be able to find more question and hopefully answers for achieving this in Unity with a thorough search.

Show more comments
avatar image Salmjak · Jul 25, 2016 at 11:02 AM 2
Share

@wottf Since I've been playing around a lot with procedural generation I can tell you that a cellular automata will give you worms-like terrain! :)

Here is a good guide to the algorithm.

So just iterate your array of white noise through this algorithm and you will get "worms-like" caves.

Depending on the "resolution" (the size) of your array you should look in to using chunks (its just splitting the terrain into equal bits and using these bits to run updates on), because updating a terrain that is based on an array[1920,1080] would probably freeze up the system for a bit.

You should also take a look on the marching squares algorithm that will help you generate your mesh as well as keep your corners smooth!

Conslusion: Fill your array with random noise ---> Iterate through the cellular automata algorithm ---> process the array through marching squares algorithm to create mesh --> put texture on mesh.

avatar image realtomshar Salmjak · Jul 25, 2016 at 11:59 AM 0
Share

@salmjak thanks for your response. I actually tried exactly what you said yesterday and the marching squares are not 1 pixel per or how can I make it smaller? Also it wasn't smooth. All times are straight or at a 45deg angle. I can't use linear interpolation because all the walls have the same value.

avatar image AlucardJay · Jul 25, 2016 at 08:21 PM 0
Share

You may want to check this thread in the forum : http://forum.unity3d.com/threads/pixel-destruction-per-pixel-2d-destruction-with-unity.198919/ (it looks like the original code has been removed, but there is a comment with another branch).

Based on : http://gamedevelopment.tutsplus.com/tutorials/coding-destructible-pixel-terrain-how-to-make-everything-explode--gamedev-45

Searching 'Unity destructible pixels' also yields results, eg: http://stackoverflow.com/questions/2083390/worms-style-destructible-terrain

avatar image realtomshar AlucardJay · Jul 26, 2016 at 06:20 AM 0
Share

@alucardj Thanks for those links, that first one seem especially interesting.

avatar image realtomshar AlucardJay · Jul 28, 2016 at 07:02 PM 0
Share

@alucardj I have played around with some ideas you linked but im not very happy with the performance of Texture2D's SetPixel, SetPixels or SetPixels32. $$anonymous$$y Textures are power of 2 and small (one per 'chunk'). Is there an alternative?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by nebur7420 · Nov 29, 2018 at 09:49 AM

I know this is a bit late and you probably already found a way but I post this for the people who are still searching for an awnser.

I have been looking into this for about a day and found a way to do it in 2d.

First you have to make 2 objects, 1 for the terrain 1 for the damage

Give them Composite Colliders. Add all the terrain colliders to the composite collider of the terrain object. And do the same for the explosions when they are created.

Now when a rocket is fired give him the following code:

 public Collider2D world;
 public Collider2D explosionDamage;
 public GameObject explosion;
 
 void Update () {
     if(_barrier.OverlapPoint(transform.position) && !_explosionDamage.OverlapPoint(transform.position)) {
         GameObject spawnedExplosion = Instantiate(_explosion, transform.position, Quaternion.identity, _explosionDamage.transform);
         Destroy(this.gameObject);
     }
 }

Now add a sprite mask to the explosion objects and say that the world can only be seen outside of the explosion.

I hope this helps and I hope you don't mind my spelling mistakes.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nebur7420 · Nov 29, 2018 at 02:07 PM 0
Share

I made this to show what it looks like This

avatar image ppehkone · Feb 01, 2019 at 09:55 AM 1
Share

Wouldn't that only create two colliders, one visible for the terrain, and another invisible one for the explosions? I mean how do you prevent the collisions on the terrain collider at the destroyed parts? You can cut parts of the terrain sprites with the sprite mask but you can't cut away parts of a collider with a sprite or another collider, can you?

I would really like to try this but I don't think I understand your solution.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I generate an omnidirectional 2d terrain? 0 Answers

2D Efficient Realtime Terrain Generation 0 Answers

How to create destructible tile based 2D terrain like 'Broforce' does ? 2 Answers

2D Procedural Terrain with SpriteManager 0 Answers

2D Procedural Terrain Generation + Pooling 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges