Search Unity

Procedurally generated tile map levels?

Discussion in '2D' started by awbevier, Dec 5, 2013.

  1. awbevier

    awbevier

    Joined:
    Mar 19, 2013
    Posts:
    10
    What is the best approach to take if I want to procedurally generate tile map levels for my game?

    Should I create a prefab for each tile type and then instantiate each prefab during my generation algorithm, or is there a way to combine all of the tile sprites into a single image and put that image on one single game object?

    If I use one gameobject per tile then I could end up with hundreds and hundreds of game objects which seems like it wouldn't be efficient.
     
  2. charl13dontsurf

    charl13dontsurf

    Joined:
    Oct 10, 2013
    Posts:
    2
    Hey,

    I am interested if someone can respond to this. I'm pretty new to working with these tools. I wanted something to do some prototyping so I started with construct 2, then game maker but I found that actually coding in C# makes a lot more sense than navigating through the clicking/scripting in those other tools.

    So my rough start was just to get a map laid out. I have a prefab sprite with some scripts and then have a game object that is dedicated to the stuff around generating my map. Basically it instantiates the prefab in a grid and the start on each clone/instance of the prefab loads up the properties of each tile (so the sprite image, calls to the tile object that has all the meta data and such). Let's call it 32x18 instances of that prefab.

    However, as you say - if you wanted to make a large map using this type of technique you end up with waaay too many objects.

    So what I started to do was I have a rough 16:9 grid that fills the ~16:9 viewport. This isn't a side scroller so performance isn't that important. There isn't any sort of rapid navigation and has a fog of war type deal. So once you move the main player sprite towards another piece of the map (say a tile to the right), I would reload the grid off of a dataset so that it refreshes. It's a much smaller set of objects and I only change them, not having to create and re-recreate them as objects after the first instantiation?

    I've been scouring forums and articles. A lot of them are from before 4.3, so they talk about using meshes and what not. Because I am such a noob, I assumed that the new 2D stuff took care of some of that need? Is it still better to have one large mesh?

    I could use an article (not specific to unity) that explains the idea of creating the mesh but being able to mouse-over/mouse-down on specific tiles within that mesh, or making sure that the data/objects implicit to each mesh are available in a way that I can refer to specific tiles easily without a sh!tload of math haha.

    Not trying to derail your thread, but I spent all day reading yesterday for exactly what you are searching for and did not find anything definitive.

    - Harris
     
    Last edited: Dec 6, 2013
  3. awbevier

    awbevier

    Joined:
    Mar 19, 2013
    Posts:
    10
    I found the same technique on building meshes, there was a video on youtube. My big hang up now is how to handle collisions. Since some tiles are collidable and some are not, I think I will have to end up using a prefab for every tile so that I can add box colliders to the ones that need to block movement. If I could resolve the collisions myself, I could write my own algorithm and forgo the box colliders, but I'm not sure how to write my own custom collider? I was hoping there was a well defined workflow for this, since every 2d game I've worked on used a tileset.
     
  4. wh1pl4sh

    wh1pl4sh

    Joined:
    Dec 6, 2013
    Posts:
    1
  5. charl13dontsurf

    charl13dontsurf

    Joined:
    Oct 10, 2013
    Posts:
    2
    Yeah, I did see that. But essentially that is what I am doing, except using a sprite not a plane. And instantiating and placing the tiles rather than having them pre-placed by hand. He's not on 4.3, so doesn't have the benefits of the new 2D stuff.. And the raycasts and stuff don't work that way with the 2d mesh collider as mouseover seems to be best?

    RealABev - when you instantiate, you can just create the collider component on tiles that need it? I don't know what kind of overhead that has at runtime though. When I'm instantiating, I don't have much in terms of components and just add what I need depending (create the SpriteRenderer and whatever sprite its using when I generate the map for the first time)...
     
    Last edited: Dec 6, 2013
  6. awbevier

    awbevier

    Joined:
    Mar 19, 2013
    Posts:
    10
    charl13dontsurf - I was planning on just making a prefab for each tile and then I can use the designer to put box colliders on the solid tiles, but I don't know if that's the approach I will end up with. I'm still learning a lot about Unity. I'll invest more time on research this weekend.
     
  7. Yukichu

    Yukichu

    Joined:
    Apr 2, 2013
    Posts:
    420
    So I've been mulling over whether or not to release the procedural tilemapping thing I wrote for 2DToolKit tilemaps (which could, or may, be ported over to others.)

    I attached a few images. It was quite interesting to learn, but a pain to debug at times. All the little sparkly things in there are enemies that have been spawned from prefabs shooting in various ways (I need to deactivate them until you're closer... I know) but the whole map gets drawn up, walls are created (and an option for no walls), and so on. There's the start room, boss/end room, variations/special instructions for them, some other nifty things.

    I still need to make the corridors 'bend' but for the most part, I kind of like it. Maybe not rocket science, but it works well.


    $ProcTileMap1.png $ProcTileMap2.png $ProcTileMap3.png
     
  8. MattMirrorfish

    MattMirrorfish

    Joined:
    Dec 8, 2013
    Posts:
    40
    If anyone is looking for a 2D level generator asset that works with Tilemaps I made one called Strata Easy 2D Procedural Level Generator:

    http://www.mirrorfishmedia.com/strata/

    Strata is an easy to use procedural toolkit for generating 2D levels which use a combination of hand-authored and procedurally generated content. It works at edit time or at runtime without additional programming. Strata works with Unity’s native 2D Tilemap tools allowing you to hand-author pieces of levels and arrange and combine those pieces in interesting ways.

    The design philosophy of Strata is strongly inspired by both classic roguelike dungeon crawler design approaches along with the newer school of action roguelike / roguelite games. To this end, the project contains demos both for a classic top-down roguelike dungeon generator, and a 2D platformer built from a connected chain of handmade rooms. Both serve as good starting points for your own creations.

    Programming procedural level generation tools can be complex and difficult, and with Strata, I hope to open the creation of procedural content to artists, level and game designers and other non-programmer creators.
     
    Lars-Steenhoff likes this.