Search Unity

Placement of different objects in tiles

Discussion in '2D' started by yigiterkal, Aug 23, 2016.

  1. yigiterkal

    yigiterkal

    Joined:
    Aug 23, 2016
    Posts:
    12
    Hello,

    I am totally beginner in Unity just started a week ago. I would like you to help me or show a waypoint in a crucial problem in mu project. I place towers in map and one of the object size is 2x2 but my map created by 1x1 tiles.

    So, I would like to see when I place a 2x2 object I can't place any object in this 2x2 area. Hopefully screenshot will show you. Sorry for my English.

    s.PNG
     
  2. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    Different ways to do it.

    Since you seem to be placing oversize Sprites on a tile map... but I don't know whether the "grid" background is truly done in a tile map editor or is just completely visual and has no map data structure backing it up...

    Just create what I call a shadow map data structure representing the buildings. All you need is an array of Int. Such as

    Code (CSharp):
    1. private int[] oBuildingsMap;
    Of course you could make that a 2-dimensional array but I just use a 1-dimensional array.

    Then you will need to create it (do an oBuildingsMap = new Int[mapWidth * mapHeight]) and you should also create a clear method that simply loops through all elements and sets them to 0. 0 meaning each cell is empty.

    When the player places a building set a value in the appropriate 4 cells in the map to reference the building (the reference value could be an index in some list of buildings or it could be the Unity InstanceID of the GameObject sprite). Each building you place will set 4 cells of your shadow map to the same value. And you can check those 4 cells to determine if any of them are already occupied so you know if this is a valid location to build or not.

    Another way you could do this is to actually use tiles for the buildings. Instead of using a single big image use 4 smaller images.

    Without knowing exactly what you're doing I'd lean toward the shadow map data structure (array) that simply tells you which cells are already occupied and which building is occupying those cells. It's always good to have data structures backing up your visual data. Or at least I think so. Visual data is meaningless to me. I need something I can work with, query, update, etc.
     
    yigiterkal likes this.
  3. yigiterkal

    yigiterkal

    Joined:
    Aug 23, 2016
    Posts:
    12
    Thank you Benjamin it helped me a lot!
     
    GarBenjamin likes this.