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

Generated mesh doesn't get the proper MeshCollider.sharedMesh

Discussion in 'Scripting' started by freddannn, Jan 7, 2016.

  1. freddannn

    freddannn

    Joined:
    Jan 2, 2016
    Posts:
    6
    So I learnt you can generate a mesh in Unity, pretty cool! The only problem I have is when I try to add a MeshCollider. As you can see in the screenshot below, the collider is added but it only appears to draw the surface between the peaks of the mesh. Any ideas on how to address that? Am I doing something wrong during generation?
    upload_2016-1-7_16-45-16.png

    I use this to attach the collider
    Code (csharp):
    1.  
    2.         meshCollider = root.AddComponent<MeshCollider> ();
    3.         meshCollider.convex = true;
    4.         meshCollider.sharedMesh = mesh;
     
  2. kietus

    kietus

    Joined:
    Jun 4, 2013
    Posts:
    54
    Hello,

    Usually adding a MeshCollider works well. You don't need to explicitly specify a mesh for the collider it will create it automatically. Remove the last line it may works.
     
  3. freddannn

    freddannn

    Joined:
    Jan 2, 2016
    Posts:
    6
    Thanks. Tried it but it didn't change anything. Here's the source:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GenerateTerrain : MonoBehaviour
    5. {
    6.     public Vector3 size = new Vector3(20f, 20f, 2f);
    7.     public float NoiseSize = 10.0f;
    8.  
    9.     private float _randH;
    10.     private float _randN;
    11.  
    12.     private GameObject terrain;
    13.     private Mesh mesh;
    14.     private MeshCollider meshCollider;
    15.  
    16.     // create the objects and add components
    17.     void Start() {
    18.         terrain = new GameObject("test");
    19.         terrain.transform.position = Vector3.zero;
    20.         terrain.transform.parent = transform;
    21.         mesh = terrain.AddComponent<MeshFilter> ().mesh;
    22.         terrain.AddComponent<MeshRenderer> ().material.color = Color.white;
    23.         meshCollider = terrain.AddComponent<MeshCollider> ();
    24.         meshCollider.convex = true;
    25. //        meshCollider.sharedMesh = mesh;
    26. //        Rigidbody rb = root.AddComponent<Rigidbody> ();
    27. //        rb.useGravity = false;
    28. //        rb.isKinematic = true;
    29.  
    30.         _randH = size.z;//*(0.5f + Random.value);
    31.         _randN = NoiseSize;//*(0.5f + Random.value);
    32.     }
    33.  
    34.     public float PerlinNoise(float x, float y) {
    35.         return _randH*Mathf.PerlinNoise(x/_randN, y/_randN);
    36.     }
    37.  
    38.     void Update(){
    39.         Generate ();
    40.     }
    41.  
    42.     // generate Perlin-mesh
    43.     void Generate(){
    44.         var vertices = new Vector3[(int) (size.x*size.y)];
    45.         var uv = new Vector2[(int) (size.x*size.y)];
    46.         var tangents = new Vector4[(int) (size.x*size.y)];
    47.  
    48.         for (int y = 0; y < size.y; y++){
    49.             for (int x = 0; x < size.x; x++) {
    50.                 float z = PerlinNoise (Time.time*1.13f + x, 1000+Time.time*0.98f + x);
    51.                 vertices[(int) (y*size.x + x)] = new Vector3 (x, y, z);
    52.                 uv[(int) (y*size.x + x)] = new Vector2(x, y);
    53.                 tangents[(int) (y*size.x + x)] = new Vector4(1f, 0f, 0f, -1.0f);
    54.             }
    55.         }
    56.  
    57.         // Build triangle indices: 3 indices into vertex array for each triangle
    58.         var triangles = new int[(int) ((size.y - 1) * (size.x - 1) * 6)];
    59.         int index = 0;
    60.         for (int y=0; y < size.y-1; y++){
    61.             for (int x=0; x < size.x-1; x++) {
    62.                 // For each grid cell output two triangles
    63.                 triangles[index++] = (int) (y     * size.x) + x;
    64.                 triangles[index++] = (int) ((y+1) * size.x) + x;
    65.                 triangles[index++] = (int) (y     * size.x) + x + 1;
    66.  
    67.                 triangles[index++] = (int) ((y+1) * size.x) + x;
    68.                 triangles[index++] = (int) ((y+1) * size.x) + x + 1;
    69.                 triangles[index++] = (int) (y     * size.x) + x + 1;
    70.             }
    71.         }
    72.  
    73.         // And assign them to the mesh
    74.         mesh.vertices = vertices;
    75.         mesh.uv = uv;
    76.         mesh.triangles = triangles;
    77.  
    78. //        Auto-calculate vertex normals from the mesh
    79.         mesh.RecalculateNormals();
    80.  
    81. //        Assign tangents after recalculating normals
    82.         mesh.tangents = tangents;
    83.     }
    84.  
    85. }
    86.