- Home /
Tile Terrain Generation
I'm trying to create a terrain out of a grid of individual 4-vertex planes which will eventually be customized for different materials(i.e. clay, dirt, sand, etc). I am able to create a grid of flat planes, but when I try to alter the height of individual vertices, only the bottom left square(Tile00) renders and the remaining are instantiated as empties.
Here is my script. I can't quite figure out what's wrong, and I'm not getting any errors other than two variables not being used.
using UnityEngine;
using System.Collections;
public class SpawnTester : MonoBehaviour {
public GameObject board;
public Transform tile_prefab_;
public int board_size_x_;
public int board_size_z_;
public int nameX;
public int nameZ;
void Start(){
GenerateTiles();
GenerateHeight();
}
void GenerateTiles () {
board = new GameObject();
board.name = "Board";
for ( int x = 0; x < board_size_x_; x++ ) {
for ( int z = 0; z < board_size_z_; z++ ) {
Transform tile = (Transform)Instantiate(tile_prefab_,new Vector3(x * 10,0,z * 10),Quaternion.identity);
tile.name = "Tile" + x + z;
tile.parent = board.transform;
}
}
}
void GenerateHeight () {
for ( int x = 0; x < board_size_x_; x++ ) {
for ( int z = 0; z < board_size_z_; z++ ) {
Transform tile = GameObject.Find("Tile" + x + z).transform;
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
if(tile.name == "Tile00"){
for(int i = 0; i < vertices.Length; i++){
vertices[i] += Vector3.up * Random.Range(0, 11);
}
mesh.vertices = vertices;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
MeshCollider meshc = tile.gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
meshc.sharedMesh = mesh;
}
else{
int tempX = x - 1;
int tempZ = z - 1;
if(tempX < 0)
tempX = 0;
if(tempZ < 0)
tempZ = 0;
string leftName = "Tile" + tempX + z;
string botName = "Tile" + x + tempZ;
Transform leftTile = GameObject.Find(leftName).transform;
Transform botTile = GameObject.Find(botName).transform;
if(leftTile){
Vector3[] leftVerts = leftTile.GetComponent<MeshFilter>().mesh.vertices;
Vector3 leftVertTop = leftVerts[0];
Vector3 leftVertBot = leftVerts[3];
vertices[1] = leftVertTop;
vertices[2] = leftVertTop;
}
else{
vertices[1] += Vector3.up * Random.Range(0, 11);
vertices[2] += Vector3.up * Random.Range(0, 11);
}
if(botTile){
Vector3[] botVerts = botTile.GetComponent<MeshFilter>().mesh.vertices;
Vector3 botVertLeft = botVerts[2];
Vector3 botVertRight = botVerts[0];
vertices[3] = botVertRight;
}
else{
vertices[3] += Vector3.up * Random.Range(0, 11);
}
vertices[0] += Vector3.up * Random.Range(0, 11);
mesh.vertices = vertices;
mesh.RecalculateBounds();
MeshCollider meshc = tile.gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
meshc.sharedMesh = mesh;
}
}
}
}
}
Answer by tylo · Oct 28, 2013 at 07:33 PM
I am not sure what it is, but there is some kind of bad logic inside of your else block starting on line 50.
I commented it out and processed all tiles as if they were treated as "Tile00" is, and the meshes were all unique (if crazy looking).
In other words, it is not a case of Unity duplicating anything internally (which can happen sometimes). All your meshes are instanced and unique.
Thanks for your response. I don't suppose you have any ideas? Also, can you kind of clarify your last line?
With things like shared$$anonymous$$aterials and shared$$anonymous$$eshes, sometimes Unity will alter every object that happens to use it. I know this is the case with shared$$anonymous$$aterials.
I'm afraid I don't have any advice on how to fix what you're trying to do in your else statement, as I am not sure what the final intent of your gameboard is supposed to look like.
All I can say for sure is, the coordinates you are giving your vertexes are incorrect, and it is causing the meshes to appear invisible. You'll have to do some serious debugging to find out what those value are being set to.
I figured that might be the case. Upon debugging the vertices, the Vector3's that I'm given are something like (1,0,-1). Is it possible that it's doing it locally in the plane from the center(origin) of the mesh? And if so, is there a way to get a global Vector3 and use that to assign the vertices.
$$anonymous$$y intent is to create a tiled terrain. If you're familiar with Wurm Online, a terrain like that. Where there is a random height to each corner and the adjacent tile will copy so that they match up perfectly.
Upon reading the Scripting Reference a bit more, would TransformPoint be what I need? And if so, how would I work it in?