I am trying to start work on learning strategy/base building games in unity. I started out by copying code from this video but my version only does a single line of squares.
It is the z dimension that does not continue and it does seem like it is a problem with the triangles/normals as the vertices seem to be in the right place via making the vertices a public variable and seeing them in unity's inspector. I think I'd have to write a widget to show the vertices to be sure as I'm no mathematician. I removed the actual creation of the object in unity for the sake of brevity as there is no math involved. Also the procedural object does seem to crumble to nothing at larger sizes, although that's not really an issue until the first one is fixed.
void BuildMesh()
{
int numTiles = size_x * size_z;
int numTris = numTiles * 2;
int vsize_x = size_x + 1;
int vsize_z = size_z + 1;
int numVerts = vsize_x * vsize_z;
//Generate Mesh Data
vertices = new Vector3[ numVerts ];
Vector3[] normals = new Vector3[ numVerts ];
Vector2[] uv = new Vector2[ numVerts ];
int[] triangles = new int[ numTris * 3];
int x, z;
for (z = 0; z < vsize_z; z++)
{
for (x = 0; x < vsize_x; x++)
{
vertices[z * vsize_x + x] = new Vector3(x * tileSize, 0, z * tileSize);
normals[ z * vsize_x + x] = Vector3.up;
uv[z * vsize_x + x] = new Vector2((float)x / vsize_x, (float)z / vsize_z);
}
}
for (z = 0; z < size_z; z++)
{
for (x = 0; x < size_x; x++)
{
int squareIndex = x * size_x + x;
int triOffset = squareIndex * 6;
triangles[triOffset + 0] = z * vsize_x + x + 0;
triangles[triOffset + 1] = z * vsize_x + x + vsize_x + 0;
triangles[triOffset + 2] = z * vsize_x + x + vsize_x + 1;
triangles[triOffset + 3] = z * vsize_x + x + 0;
triangles[triOffset + 4] = z * vsize_x + x + vsize_x + 1;
triangles[triOffset + 5] = z * vsize_x + x + 1;
}
}
The code from the video: