Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Add mesh collider to procedurally generated terrain

Discussion in 'Getting Started' started by Inferno_1424, Oct 22, 2021.

  1. Inferno_1424

    Inferno_1424

    Joined:
    Oct 21, 2021
    Posts:
    2
    Can anybody help me add a collider to this code which generates terrain at runtime?


    [RequireComponent(typeof(MeshFilter))]
    public class MeshGenerator : MonoBehaviour
    {

    Mesh mesh;

    Vector3[] vertices;
    int[] triangles;

    public int xSize = 100;
    public int zSize = 100;

    // Start is called before the first frame update
    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 = new Vector3(x, y, z);
    i++;
    }
    }

    triangles = new int[xSize * zSize * 6];

    int vert = 0;
    int 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++;
    }

    }

    // UpdateMesh is called once per frame
    void UpdateMesh()
    {
    mesh.Clear();

    mesh.vertices = vertices;
    mesh.triangles = triangles;

    mesh.RecalculateNormals();
    }

    }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Try:

    Code (CSharp):
    1. gameObject.AddComponent<MeshCollider>().mesh = mesh;
     
  3. Inferno_1424

    Inferno_1424

    Joined:
    Oct 21, 2021
    Posts:
    2
    Thanks for the suggestion, when i tried it i got this error

    Assets\Terrain\MeshGenerator.cs(22,49): error CS1061: 'MeshCollider' does not contain a definition for 'mesh' and no accessible extension method 'mesh' accepting a first argument of type 'MeshCollider' could be found (are you missing a using directive or an assembly reference?)
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Yes, that was off the top of my head, to give you push in the right direction. You're meant to go look up the relevant classes and figure out the details. In this case the only relevant class is this one.
     
  5. Capzeze

    Capzeze

    Joined:
    Mar 28, 2021
    Posts:
    3
    I think you already figured it out but just add
    Code (CSharp):
    1. gameObject.AddComponent<MeshCollider>().sharedmesh = mesh;
    at the end of UpdateMesh()