- Home /
Generate terrain tutorial by brackeys not working
So i am following the tutorial PROCEDURAL TERRAIN in Unity by Brackeys: https://www.youtube.com/watch?v=64NblGkAabk&ab_channel=Brackeys
But i am having trouble getting my code to work. The code does instantiate the spheres representing the each vertice on the mesh, it even applies the perlin noise to them. But it doesn't create the triangles and quads between the vertices, and i am suspecting it's because the video is two years old, and new updates has arrived.
Anyone who can take a look at the code, and tell me how to fix it?
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
public class MeshGenerator : MonoBehaviour
{
public int xSize = 20;
public int zSize = 20;
Mesh mesh;
Vector3[] vertices;
int[] triangles;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
CreateShape();
UpdateMesh();
}
void CreateShape()
{
vertices = new Vector3[(xSize + 1) * (zSize + 1)];
for (int i = 0, z = 0; z <= zSize; z++)
{
for (int x = 0; x <= xSize; x++)
{
float y = Mathf.PerlinNoise(x * .3f, z * .3f) * 2f;
vertices[i] = new Vector3(x, y, z);
i++;
}
}
triangles = new int[xSize * zSize * 6];
var vert = 0;
var tris = 0;
for (int z = 0; z < zSize; z++)
{
for (int x = 0; x < xSize; x++)
{
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + xSize + 1;
triangles[tris + 2] = vert + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + xSize + 1;
triangles[tris + 5] = vert + xSize + 2;
vert++;
tris += 6;
}
vert++;
}
}
private void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
void OnDrawGizmos()
{
if (vertices == null)
return;
for (int i = 0; i < vertices.Length; i++)
{
Gizmos.DrawSphere(vertices[i], .1f);
}
}
}
Thanks!
It's obvious but... does $$anonymous$$eshRenderer
exist too?
[RequireComponent( typeof($$anonymous$$eshFilter) , typeof($$anonymous$$eshRenderer) )]
Your answer
Follow this Question
Related Questions
How to make a procedural mesh that can be edited on a per- vertices basis 1 Answer
get terrain vertices? 2 Answers
64k vertices limitation 2 Answers
Build plane on quad of mesh with same vertices 1 Answer
Set Vertices to Ground 1 Answer