Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to handle buildings and "blueprints" with tilemaps

Discussion in '2D' started by Ryddave, Feb 10, 2021.

  1. Ryddave

    Ryddave

    Joined:
    Apr 15, 2020
    Posts:
    5
    Hello,

    I'm trying to make a simple 2D tilemap game and I want to add buildings to the game. However, i'm wondering what is the correct way to handle that.

    I want to handle two things:
    - building placement: the player select a building and place it on the map. Is it preferable to use a tilemap to place the building on the tile corresponding to mouse position or should I place a sprite manually avoiding an additional tilemap (which will only use one tile at the time) ?
    - building blueprint: the player places "ghost" buildings that have to be built. Should I use a tilemap for blueprints or is there a better solution ?

    Thanks for your advices.

    Rdave
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    For myself, I found that using the tilemap for the buildings was more trouble than it was worth. For instance, I had a building mechanic so I wanted to detect the player swung at a building, but since tilemaps only have a single collider, you could only detect that a collision happened somewhere on the map, not specifically which building, which meant I had to jump through a lot of hoops to figure out specifically which building was struck. That said, individual game objects for each building has its own pains, not least of which is performance. I have to dynamically figure out what needs to be seen and what doesn't and create/destroy the objects as I move around, something the tilemap would handle mostly for you.

    So, I think the big question is what sort of things do you want to do with your buildings? Can these be done easily if the buildings are on the tilemap, or are they going to be troublesome. Additionally, are you going to need to do dynamic tiling or not (ie do you have a huge continuous or a relatively small one which can be easily contained within a static tilemap).
     
  3. Ryddave

    Ryddave

    Joined:
    Apr 15, 2020
    Posts:
    5
    Hi,

    Thank for your anwser :).

    For now I maintain a data structure containing information about each tile position in parallel of the tilemap. The tilemap are updated depending on the game data. However I don't handle dynamic tiling for now and I only have a static map representation.

    I'm not sure what can the buildings do. But I think about at least those features:
    - be used by player's unit (like crafting for instance).
    - move resources on rail: similar to ONI's conveiyor rails or factorio's rails for instance, the player build rail and move some resource from one building to another. I think this part is quite harder. I'm not sure where the data of resource should be (stored in the rail or at separate gameobjects).
    - fight: military building such as bullet tower which can defend against agressive mobs.
     
  4. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    I would say of those:
    -crafting is going to be troublesome as, like with me, detecting which exact building they are accessing is going to be annoying as you only have the 1 collider to work with... HOWEVER
    -resources like factorio is going to be pretty intense to do with gameobjects for each tile. You really want the efficiency gain of the tilemap here if your map is at all large
    -this one is rather neutral. You'll know the positions of the towers from your data so you can easily have a master script that handles the spawning of projectiles.

    So, right now I'd say you're kinda down the middle. As such I'd say try tilemaps first and see how that goes. I know that's how I started and even when I switched, it was a pain but not that hard, so it isn't like you're totally committed should you run into a major issue.

    As to such, you probably are going to want 3 layers of maps: the base map, your building map, and a blueprint map which will have a special shader on it or something to show the blueprints as incomplete.
     
  5. Ryddave

    Ryddave

    Joined:
    Apr 15, 2020
    Posts:
    5
    Hi, sorry for the late anwser.

    Concerning the troublesome about crafting, I was wondering if it could be preferable to handle the "collision" with building by using position of player instead of Unity collider system.
    For instance: each time the player move, I check the position (x, y) and compare it to a list of buildings to determines if a craft table or such is on its position.
    This require to hold a separate data structure of buildings on the map. How this solution compare to tilemap based detection ?
    However, if I do the same for all building, like for walls for instance, it seems more complex than simply put walls in a tilemap.

    Concerning turret, I've some difficulties to seed hows you would handle graphic animation such as turret canon rotation with a tilemap ?

    For resource conveyor, it seems actually quite intensive to use gameobject... However i'm wondering how classifcal games handle that. Because each conveyor tile should know what type and amount of resource are stored. THe solution could be to use gameobject for the resources themselves and move those object depending on the tile one which they are ?

    Using a blueprint map seems to be the way to go. But for the building placement (only one building one the mouse when the player place it), I should spawn manually the corresponding tile ?

    Thank you very much for your advices.
     
  6. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    You can do the position checks that way, it is just a pain. Do keep in mind, though, if you have a free moving character that isn't locked to the grid, those checks are even more complex and can often feel off if they aren't done just right. It can also potentially be rather difficult if you have varied range and need to check more than the local tiles, but usually that shouldn't be an issue.

    Tiles can be animated, and I believe individual tiles can have separate animations as well, so that's probably not that hard.

    For the convey data, you'll have same data storing the gameobjects. Now, for factorio they divide their belts into 8 pieces with a couple animations, and they are noticeably more efficient at saturation with a single resource so they are doing some kind of 'cheats' as it were in said situation. So you may well want to make your tilemap actually smaller than your place-ables for this reason so you can more easily alter the segments of each conveyor. Properly divided you only have to deal with a small number of situations instead of potentially hundreds of combos.