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

Instantiate Objects on Tilemap

Discussion in '2D' started by Hilifrio, Feb 24, 2020.

  1. Hilifrio

    Hilifrio

    Joined:
    Feb 6, 2020
    Posts:
    10
    Hi evryone!
    I'm new in unity and i'm trying to develop my second game wich is a strategy game!
    My game has an isometric tile map on wich there are some randomly placed Castles. Each Castle takes 3*3 Tiles but i'm trying to find a good method to place my castles on the map and I don't really know how to do that properly.
    I want my Castle to be scriptable so I can add garnison, change it's appearance, etc.. the only way to do that i found is to instantiate a prefab GameObject with "Instantiate()" wich is my castle. However i've got out of bounds placement issues due to the unit, and the Tiling on the map isn't clean enough.
     

    Attached Files:

  2. ShiroTheWhite

    ShiroTheWhite

    Joined:
    Apr 4, 2018
    Posts:
    16
    I would position empty gameObject(s) on the location you want spawn and use their position to correctly place them with instantiate

    Another way would be to get the Length and width of the Grid Area. Get the size of a Single tile in unity's units.
    Then with the Size of one Tile find the length of the object

    for example if we have a Tile Size of 1 unit, the length would be 3 units since the castle is 3 tiles wide ;

    xTileSize = 1 unit

    Since width is also 1 tile wide

    yTileSize = 1 unit

    We will assume the we have a 36x36 Grid area. It can be any size though.

    Since unity defaults to center pivot to find the exact location to spawn castle

    void Start {
    //Castle length and width size in tiles
    CastleLength = 3; // 3 tiles long
    CastleWidth = 3; // 3 tiles wide

    //Castle length and width size in unity units
    CastleTileLength = CastleLength * xTileSize;
    CastleTileWidth = CastleWidth * yTileSize;

    //Center the first coordinates
    Xcord += CastleTileLength * 0.5f;
    YCord += CastleTileWidth * 0.5f;
    }

    in some other function you can do

    //Find how much castles we can fit
    NoOfRows = 36 / CastleLength; castles per row
    NoOfCols = 36 / CastleWidth; castles per col

    //Basically get the top left position of the grid to start spawning from
    Xcord = Grid.transform.position.x - (Grid.transform.position.localscale.x);
    Ycord = Grid.transform.position.y - (Grid.transform.position.localscale.y);

    Then in some other funtion do this
    for(int j=0; j < NofCols; j++)
    {
    for(int i 0; i < NoOfRows;i++)
    {
    SpawnCastle(XCord, YCord);
    XCord += CastleTileLength;
    }
    YCord += CastleTileWidth;
    }
     
    Last edited: Feb 24, 2020
  3. Hilifrio

    Hilifrio

    Joined:
    Feb 6, 2020
    Posts:
    10
    Thanks for your answer! I'll try this, so in this example my Castle will be an empty GameObject? I just don't figure out how to convert my tiles units into unity units..
    I also saw the tilemap.SetTile() method wich is easier than instantiate for placing items on tilemaps. However it seems not possible to script a tile as I want to script my castles is it?
     
  4. ShiroTheWhite

    ShiroTheWhite

    Joined:
    Apr 4, 2018
    Posts:
    16
    Unity units is basically how big the tile is in the scene (Scale on the x and y axis). So if your castle is 3 tiles wide then it would be 3 x Scale to get the length/width