Tile script generates cubes too close together

I’m trying to make a tactics based RPG along the same vein as Final Fantasy Tactics and I found a great scrip that works well for my purposes. Unfortunately, I don’t really understand it very well and I’ve been trying to fiddle with it so that I can learn how it functions. It takes a prefab object and tiles it together. When I tried to make the prefab twice the size, it still places them the same distance apart as before, making them overlap. Is there anywhere in the code I can fix this? Can anyone help me understand what in the world is going on in here?

From my manager script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Manager : MonoBehaviour {
public GameObject TilePrefab;
public int mapSize = 11;
List <List> map = new List<List>();

// Use this for initialization
void Start () {
generateMap ();
}

// Update is called once per frame
void Update () {

}

void generateMap(){
map = new List<List> ();
for (int i = 0; i < mapSize; i++) { //This loop will run until i=mapSize, adding one each time. i is given the value 0, then used just to tell the loop when to stop.
List row = new List ();
for (int j = 0; j < mapSize; j++) {
Tile tile = ((GameObject)Instantiate (TilePrefab, new Vector3 (i - Mathf.Floor (mapSize / 2), 0, -j + Mathf.Floor (mapSize / 2)), Quaternion.Euler (new Vector3 ()))).GetComponent ();
tile.gridPosition = new Vector2 (i, j);
row.Add (tile);
}
map.Add (row);
}
}
}

In my tile script:
using UnityEngine;
using System.Collections;

public class Tile : MonoBehaviour {

public Vector2 gridPosition = Vector2.zero;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
void OnMouseEnter(){
transform.renderer.material.color = Color.blue;
Debug.Log (“my position is (” + gridPosition.x + “,” + gridPosition.y);
}
void OnMouseExit(){
transform.renderer.material.color = Color.white;
}
}

They’ve gone out of their way to make the map size equal to the grid unit size of Unity. Why can’t you just move closer or further away?

I totally can, but I was trying to get a better grasp of what his code is actually doing by messing with it.

This line:

tile.gridPosition = new Vector2 (i, j);

Is what is turning your integer values into world positions.

You can multiply that vector by a scale factor first, then assign it to your new objects.

1 Like

Looking further down the line, I find that making a single centralized static CalculatePosition() function is very handy, such that every object and entity in the game uses it in order to translate map coordinates into the unity world space.

And of course a function that goes the opposite way is often handy too: takes a Vector3 and returns an integer pair x,y, of course bounds-checked to make sure you’re still “on map.”