# How can I make some tiles on a mesh higher up than others?

For the record, I’m using C#.

I’m completely lost with what I should do here - I’ve got code that can generate a plane mesh of arbitrary size, but I don’t know where to go from here. I want to be able to make it so that some tiles (made of two triangles at this point) are higher than others. Given a multidimensional array of X,Z coordinates, with a binary flag for whether or not the tile is a wall, how can I make the code I have raise some tiles up in the air to form walls? I’m primarily experienced in making 2D games, and this is the first time I’ve ever attempted procedural generation in a 3D game, and one of the first 3D projects I’ve ever worked on.

I hope I’m making sense. If not, here’s a glorious MS Paint illustration:

Just imagine you’re looking at a cross section of a level from the side.

Below is the code I have:

`
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Generate : MonoBehaviour {

``````public int sizeX = 50;
public int sizeZ = 50;
public float tileSize = 1.0f;

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

public void BuildMesh() {
int numTiles = sizeX * sizeZ;
int numTris = numTiles * 2;
List<int[]> map = new List<int[]>();

//Create a map of all tile coordinates, with binary (0, 1) for whether or not the tile in question is a wall.
for(int xi = 0; xi < sizeX; xi++){
for(int zi = 0; zi < sizeZ; zi++){
int[] xz = new int[3];
xz[0] = xi;
xz[1] = zi;
xz[2] = Random.Range (0, 1);
Debug.Log ("Tile " + xz[0] + ", " + xz[1] + " mapped!");
}
}

int vSizeX = sizeX + 1;
int vSizeZ = sizeZ + 1;
int numVerts = vSizeX * vSizeZ;

// Initialize the arrays that will hold our mesh data.
Vector3[] 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 < vSizeZ; z++) {
for(x=0; x < vSizeX; x++) {

vertices[ z * vSizeX + x ] = new Vector3( x*tileSize, 0, -z*tileSize );
normals[ z * vSizeX + x ] = Vector3.up;
uv[ z * vSizeX + x ] = new Vector2( (float)x / sizeX, 1f - (float)z / sizeZ );

}
}
Debug.Log ("Vertices done.");

for(z=0; z < sizeZ; z++) {
for(x=0; x < sizeX; x++) {
int squareIndex = z * sizeX + x;
int triOffset = squareIndex * 6;
triangles[triOffset + 0] = z * vSizeX + x + 0;
triangles[triOffset + 2] = z * vSizeX + x + vSizeX + 0;
triangles[triOffset + 1] = z * vSizeX + x + vSizeX + 1;

triangles[triOffset + 3] = z * vSizeX + x + 0;
triangles[triOffset + 5] = z * vSizeX + x + vSizeX + 1;
triangles[triOffset + 4] = z * vSizeX + x + 1;
}
}

Debug.Log ("Triangles done.");

// Now, we create a mesh and then use the data we just generated to populate it.
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
mesh.uv = uv;

// Then we fill the mesh filter and collider with the mesh we just created.
MeshFilter mesh_filter = GetComponent<MeshFilter>();
MeshCollider mesh_collider = GetComponent<MeshCollider>();

mesh_filter.mesh = mesh;
mesh_collider.sharedMesh = mesh;
Debug.Log ("Mesh Done.");

}
``````

}

`

This line:

`````` vertices[ z * vSizeX + x ] = new Vector3( x*tileSize, 0, -z*tileSize );
``````

is the one you should look at. It created a new Vector3 and adds this V3 to the array “vertices” which later gets used to build triangles and thus the mesh. So the new Vector3 is in effect the position of this specific vertex. The x and z positions should be self-explanatory, but look at the y positition: It is 0 for all vertices that are added in the for loop. You just change this y position from 0 to, let’s say 1 for whichever vertices you want to be higher. Of course, you need to visualize how the vertices are arranged so that you know exactly which vertices need to be higher. Remember that each triangle is made up of three vertices, and each quad is made up of two triangles.

If you want to only make those vertices that are on a position flagged as a wall, you might have to add those vertices seperataly because otherwise you won’t have 90 degree wall angles from the floor up, but rather slopes (You’ll see what I mean when you keep the number of vertices).