Home > Back-end >  Function for changing a tile to multiple tiles Ex: 5x7
Function for changing a tile to multiple tiles Ex: 5x7

Time:07-16

I am working in unity (C#) and I am placing tiles on my tile map and then separating them. Once they are separated I need to turn them into "rooms" or squares. How can I do this starting from the middle tile (origin/the only tile)

CodePudding user response:

I found the answer! with the function below you can have what you ask for. In practice, it builds line by line, starting from the bottom. When he got the vector of a cell, he adds 0.5f or 1 (depending on whether it's even or odd) to each coordinate, and subtracts half of the "size" vector. This way the center will always be on (0, 0) and the other vectors will be as in the example you did.

Script:

public void Calculate(Vector2 size)
{
    List<Vector2> results = new List<Vector2>();
    for (int i = 0; i < size.y; i  )
        for (int o = 0; o < size.x; o  )
            results.Add(new Vector2(o, i)   (new Vector2(size.x % 2 != 0 ? .5f : 1, size.y % 2 != 0 ? .5f : 1) - (size / 2)));
    string st = "";
    for (int i = 0; i < results.Count; i  )
        st  = "\n"   results[i].ToString();
    print(st);
}

void Start()
{
    Calculate(new Vector2(5, 7));
}

Output for Calculate(new Vector2(5, 7)), as the question title: (-2.00, -3.00) (-1.00, -3.00) (0.00, -3.00) (1.00, -3.00) (2.00, -3.00) (-2.00, -2.00) (-1.00, -2.00) (0.00, -2.00) (1.00, -2.00) (2.00, -2.00) (-2.00, -1.00) (-1.00, -1.00) (0.00, -1.00) (1.00, -1.00) (2.00, -1.00) (-2.00, 0.00) (-1.00, 0.00) (0.00, 0.00) (1.00, 0.00) (2.00, 0.00) (-2.00, 1.00) (-1.00, 1.00) (0.00, 1.00) (1.00, 1.00) (2.00, 1.00) (-2.00, 2.00) (-1.00, 2.00) (0.00, 2.00) (1.00, 2.00) (2.00, 2.00) (-2.00, 3.00) (-1.00, 3.00) (0.00, 3.00) (1.00, 3.00) (2.00, 3.00)

  • Related