private float Intersection(Vector2 start, Vector2 dir, float distance) { int x = Mathf.FloorToInt(start.x/CELL_SIZE); int y = Mathf.FloorToInt(start.y/CELL_SIZE); float nearXPlane, nearYPlane; int stepX, stepY; if(dir.x >= 0) { nearXPlane = (x+1) * CELL_SIZE; stepX = 1; } else { nearXPlane = x * CELL_SIZE; stepX = -1; } if( dir.y >= 0 ) { nearYPlane = (y+1) * CELL_SIZE; stepY = 1; } else { nearYPlane = y * CELL_SIZE; stepY = -1; } float maxX = (nearXPlane - start.x)/dir.x; float maxY = (nearYPlane - start.y)/dir.y; float deltaX = CELL_SIZE/Mathf.Abs(dir.x); float deltaY = CELL_SIZE/Mathf.Abs(dir.y); float dis = 0; while( dis < distance ) { DrawTile(x, y); dis = Mathf.Min( maxX, maxY ); if(maxX < maxY) { maxX += deltaX; x += stepX; } else { maxY += deltaY; y += stepY; } if( IsSolid(x, y) ) return dis; } return float.MaxValue; }