Search Unity

Tile Map I need a some help getting tile position

Discussion in '2D Experimental Preview' started by DDaddySupreme, Jul 16, 2017.

  1. DDaddySupreme

    DDaddySupreme

    Joined:
    Jun 3, 2017
    Posts:
    10
    I've got one tilemap for walls, and one for floors, both under the same grid. What I'm trying to do is put a script onto a tilemap and have it spawn enemies randomly on that tilemap, like random spawning in Earthbound.

    The problem is, I don't know how to get the position of a specific tile. I was trying to use:

    public TileMap tileMap;
    foreach (Tile tile in tileMap.GetTilesBlock(tileMap.cellBounds))
    { // convert matrix4x4 to vector3 and add to list }

    but for some reason, when I Debug.Log the transform of 'tile', it shows the current amount of tiles, and if I use an if statement it can tell me how many are null, but the transform is always the same, and doesn't seem to change depending on which tile it's currently processing.
    Also it's a matrix4x4 which I have no experience with at all, and there's not much documentation so I'm probably not even going about this in the right way. Any help figuring out how to get a list of all used tile positions, or a clever workaround would be much appreciated.
     
  2. danbrani

    danbrani

    Joined:
    Nov 22, 2012
    Posts:
    45
    rakkarage likes this.
  3. DDaddySupreme

    DDaddySupreme

    Joined:
    Jun 3, 2017
    Posts:
    10
    Thanks for the reply! It wasn't exactly what I was looking for, but my code was a lost cause. I think I was trying to get the position of a tile from the tile prefab... But now I've fixed my code up a bit, and using CellToWorld does do exactly what I need it to!
     
  4. bmaster3113

    bmaster3113

    Joined:
    Sep 3, 2018
    Posts:
    1

    How did you change your code?

    I'm trying a similar thing I'm using tile maps with specific sprites of enemies as spawning locations for the real enemies to be created at. But I can't find a way to get the position of the specific tiles.

    I know that the position is used in some functions as GetTilesBlock(boundsInt) wouldn't be functional if it couldn't detect the position of a tile to know its within the bounds.