after lots of tutorials and searching I seem to be stuck on the spacing of the game objects created in a grid. with the below code I can get a grid of my choosing(cols, rows) but cannot change the spacing.
public int cols;
public int rows;
public GameObject tile;
// Use this for initialization
void Start ()
{
for(int y=0;y<cols;y++)
{
for(int x=0;x<rows;x++)
{
GameObject g = Instantiate(tile,new Vector3(x,y,0),Quaternion.identity)as GameObject;
g.name = x + "/" + y;
g.transform.parent = gameObject.transform;
}
}
gameObject.transform.position = new Vector3(-2.5f,-2f,0);
}
being new to programming I am finding it hard to find the right references or tutorials to help me on my way, any tutorials for this would better but if code is provided please provide a small explanation to the change so I learn.
the whole logic where the tile is spawned is in the creation of the Vector spawnPos.
public int cols;
public int rows;
// spacing variable
public float Xspace;
public float Yspace;
//point where the first tile is made
public float Xstart;
public float Ystart;
public GameObject tile;
// Use this for initialization
void Start ()
{
for(int y=0;y<cols;y++)
{
for(int x=0;x<rows;x++)
{
Vector3 spawnPos = new Vector3 (Xstart+ x*(1+Xspace) , Ystart + y*(1+Yspace) , 0);
GameObject g = Instantiate(tile, spawnPos , Quaternion.identity)as GameObject;
g.name = x + "/" + y;
g.transform.parent = gameObject.transform;
}
}
}
tip for further programing, here some remarks on your code:
Its inefficent to first make tons of tiles/blocks and then move them instead of having them spawn at the right place from the beginning.
The vector (-2.5, 2,0 ) is hardcoded, thats very bad practise. Have a variable vector or variables x,y to change the position of the total field. (In my code done by variables of the starting point)
1 more ultra tip:
When confronted with a question like you have: “how do i make spacing?”. Your thought should be something like this:
spacing is about the position > so i want to manipulate the position > where is decided, where the tiles get positioned? > oh, its the vector(x,y,0) > so i have to replace that vector with something that has spacing > try out some other vector ^^
You are instantiating objects in a double for loop with this code.
You can do the same with floats, for example. Here is the code.
[SerializeField]
public int cols = 1;
[SerializeField]
public int rows = 1;
[SerializeField]
private float horizontalSpacing = 1f;
[SerializeField]
private float verticalSpacing = 1f;
public GameObject tile;
void Start ()
{
/*
for(int y=0;y<cols;y++)
for(int x=0;x<rows;x++)
{
GameObject g = Instantiate(tile,new Vector3(x,y,0),Quaternion.identity)as GameObject;
g.name = x + "/" + y;
g.transform.parent = gameObject.transform;
}
*/
for(float x = 0f; x < cols * horizontalSpacing; x+= horizontalSpacing)
for(float y = 0f; y < rows * verticalSpacing; y+= verticalSpacing)
{
GameObject g = Instantiate(tile,new Vector3(x,y,0),Quaternion.identity)as GameObject;
g.name = x.ToString() + "/" + y.ToString();
g.transform.parent = gameObject.transform;
}
gameObject.transform.position = new Vector3(-2.5f,-2f,0);
}
Feel free to ask if you have any questions left.
if you think 2 solutions are not enough …
using UnityEngine;
public class GenerateTilemap : MonoBehaviour {
[Tooltip("the tile with the lowest x and y positions")]
public Vector3 StartPosition;
[Tooltip("Number of Tiles in this dimension")]
public int ColCount = 1, RowCount = 1;
public Vector2 Spacing;
public Vector2 Tilesize;
public GameObject TilePrefab;
public GameObject[,] Tilemap;
void Start() {
if (TilePrefab == null) {
Debug.LogError("a prefab needs to be set");
}
Tilemap = new GameObject[RowCount, ColCount];
for (int x = 0, y = 0; y < RowCount; x = (x + 1) % ColCount, y += x == 0 ? 1 : 0) {
var pos = StartPosition + x * Vector3.right *(Tilesize.x + Spacing.x)
+ y * Vector3.up * (Tilesize.y + Spacing.y);
Tilemap[y, x] = (GameObject) Instantiate(TilePrefab, pos, Quaternion.identity);
Tilemap[y, x].name = "Tile x | y";
Tilemap[y, x].transform.SetParent(this.transform, true);
}
}
}