Search Unity

Determining which grid tiles a straight line passes over on a tilemap

Discussion in '2D' started by RidgeWare, Dec 4, 2018.

  1. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    67
    Hello all,

    I'm familiar with RayCastAll, which is great for tracking which game objects a line passes through, but I'm struggling to work out the best way to monitor which grid tiles on a tilemap a straight line passes over.

    The reason I need to know this is down to working out Line of Sight. If a room has multiple pillars, each of which use a Tilemap Collider, there is a problem where RayCastAll only returns the first pillar it comes across - and ignores the others.

    Which is not robust enough. I need to know *all* instances of tilemap walls/pillars along a line.

    Is the only option going to be doing a Mathf.FloorToInt on a loop every few dots along the distance from the Ray start to end point? Then checking my tile array for the tile type?

    I guess that would work, but it seems a rather messy long-winded solution.
     
    Last edited: Dec 4, 2018