- Home /
"Debugging Question"
Procedural Generation
Well, I have knowledge of have but I'm new on C# and Unity, I've been trying to follow a tutorial wich generates a procedural terrain creating an empty object and modifying its mesh, I know this is probably a frecuently asked question but I couldn't find the solution, thanks in advance. This is the object wich has the script:
This is how it looks once the game starts:
And the Code:
int Width;
public Material floorMaterial;
void Start(){
Mesh mesh = new Mesh ();
MeshRenderer renderer = GetComponent<MeshRenderer> ();
Width = 10;
int Samples = 5;
float edge = Width / (Samples - 1);
// Create the vertices and texture coords
var vertices = new Vector3[Samples * Samples];
var uvs = new Vector2[Samples * Samples];
// Fill in the data
for (int i = 0, p = 0; i < Samples; i++) {
for (int j = 0; j < Samples; j++) {
// Current vertex
var center = new Vector3(i * edge, 0, j * edge);
// Height of this vertex (from heightmap)
float h = SampleHeight(center.x, center.z);
center.y = h;
vertices[p] = center;
// UV coords in [0,1] space
uvs[p++] = new Vector2(i/(Samples - 1f),
j/(Samples - 1f));
}
}
GetComponent<MeshFilter> ().mesh = mesh;
mesh.vertices = vertices;
mesh.uv = uvs;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.name = "Terrain";
renderer.material = floorMaterial;
}
public float SampleHeight(float x, float y) {
return Mathf.PerlinNoise(x, y);
}
You don't actually ask a question, and you don't say what the problem is.
Hi, you defines a list of vertices and UVs, but no triangles!
Actually this question should have been rejected by our $$anonymous$$oderator Guidelines since it's a typical "Debugging Question". Also it's nice that you mentioned that you "follow a tutorial" but you did not mentiond which one.
Follow this Question
Related Questions
Create the visual spring in Unity? 3 Answers
Procedural Cylinder Generation 2 Answers
Procedural generated mesh problem 1 Answer
Procedural mesh creation issue 1 Answer
Generate mesh from raycast positions, independent of rotations 0 Answers