Search Unity

Tilemap and unit positions/collisions

Discussion in 'Scripting' started by Teknoman, Sep 6, 2017.

  1. Teknoman

    Teknoman

    Joined:
    May 7, 2017
    Posts:
    22
    Hi,

    I have a tilemap, which basically is some kind of matrix with tiles. To access a tile, you access that position in the map matrix with integer values. How are the unit positions usually implemented, so you can handle units that occupy multiple tiles at once (because they are moving between tiles), and so that collision detection works? Likewise, how to handle buildings that spawn several tiles?

    I want to know on a conceptual level, no actual code is required.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You just need to convert from world space to tile space:

    Code (csharp):
    1.  
    2. tileX = (int)(worldX / TILE_WIDTH)
    3. tileY = (int)(worldY / TILE_HEIGHT)
    4.  
    If units really need to consider multiple tiles (very, very rare), you can just the extent of their sprite dimensions to get a range of tiles.
     
  3. Teknoman

    Teknoman

    Joined:
    May 7, 2017
    Posts:
    22
    Consider games with big epic units (for example SC2 or CnC3, etc), they have big units that visually occupy multiple tiles. Most games just handle them as occupying a single tile though...

    The problem is this: if a projectile impacts on the ground at tile x,y and you try to check if there is a unit there, you might get a false result because the unit is only in a single tile (the center of the sprite/model), say at x+1,y. However, the visual model of the unit might still overlap tile x,y.