public static TileBase GetTileFromCollider(TilemapCollider2D col, Vector2 point) { var tileMap = col.GetComponent <Tilemap>(); var grid = tileMap.layoutGrid; var pos = grid.WorldToCell(point); return(tileMap.GetTile(pos)); }
public void UseThis() { MousePosChecker(); // Centering the position Vector2 newPos = new Vector2(point.position.x, point.position.y) - new Vector2(0.5f, 0.5f); newPos = new Vector2(Mathf.CeilToInt(newPos.x), Mathf.CeilToInt(newPos.y)); // Checking if we are in the tilemap bool contacpoints = tilemap.GetComponent <TilemapCollider2D>().OverlapPoint(newPos); if (contacpoints) { // Chanching from float vector to int vector Vector3Int hoedTilePos = new Vector3Int((int)newPos.x - 1, (int)newPos.y - 1, 0); // Cheching if this is tile is unhoed tile. TileBase tileToCheck = tilemap.GetComponent <Tilemap>().GetTile(hoedTilePos); if (tileToCheck == unHoedTile) { // replaceing tile with new tile tilemap.GetComponent <Tilemap>().SetTile(hoedTilePos, hoedTile); } } }