Hello guys, im creating a game inspired by a mod from Warcraft 3 called warlocks. Im making the game in 2d with free sprites from the asset store. Currently im trying to make a ground that gets “destroyed”.

Im swapping the green tiles with the red tiles(they are seperate tilemaps), so it looks like im destroying the ground. But I only managed to swap only the top side of the tilemap. Code below:

    private Tilemap tilemap;
    private int xValue;
    private int yValue;
    public Tile lava;

    // Start is called before the first frame update
    void Start()
    {
        //get the big red tilemap
        tilemap = GetComponent<Tilemap>();

        //x and y in the tilemap should always be uneven, so that the (0,0) position of the tilemap is always in the middle
        //The -1 is the (0,0) position on the tilemap
        xValue = (tilemap.size.x - 1) / 2;
        yValue = (tilemap.size.y - 1) / 2;

        //Invoking the GroundDestroy method with no wait time, then repeating every second. 
        InvokeRepeating("GroundDestroy", 0.0f, 1.0f);
    }

    //Method to destroy the ground
    private void GroundDestroy()
    {
        //This starts at the top of the tilemap, and goes down until it reaches the middle, in  other words y=0
        for (int x = -xValue; x <= xValue; x++)
        {
            if (yValue > 0)
                tilemap.SetTile(new Vector3Int(x, yValue, 0), lava);
            else
                //Canceling the invoke after half the ground is destroyed
                CancelInvoke();     
        }
        yValue--;
        Debug.Log("Ground is shrinking!");
    }

My goal is to swap the tiles(the green ones of course) on all four side simultaneously, and I dont know how to do that. Any help will be much appreciated. Thank you for your time.

Your current loop currently iterates from -(width-1) to width-1. This is not what you want.

You want either:

  • 4 loops, each removing a line from one of the side.
  • A function that takes a position and removes all the squares on that line/column, that is then called on the corners.
  • A function that decides from the coordinates if a square should be removed or not, and run it on every square, using a nested loop.

Try to think about each line you want to remove, and what defines them.