Search Unity

Procedural MeshCollider not active?

Discussion in 'Scripting' started by maikkel, Apr 16, 2011.

  1. maikkel

    maikkel

    Joined:
    Sep 24, 2010
    Posts:
    20
    Hi all

    I'm generating a Terrain procedurally, from Tiles which i then combine to Chunks (like Minecraft, but 2d, Heightmap).

    I generate the Mesh for each Tile and use it as a MeshCollider (The tile does not have a mesh renderer since the mesh then gets combined in the chunk for rendering). The rendering works perfectly, but the MeshCollider doesnt react to the OnMouseDown() function when I Run the game.

    Strangely, when I then in the Editor uncheck and check the "trigger" property (or anything else) on the MeshCollider while the game is running, it works in the Game View and the OnMouseDown-Function works as well.

    I tried setting the "Trigger" to false and then to true in code, but this still doesnt help... Is the MeshCollider somehow not updated right, or did i Miss something?

    Here's the Code that generates the MeshCollider:

    Code (csharp):
    1.  
    2.            Vector3[] p = new Vector3[9];
    3.  
    4.             p[0] = new Vector3(block.y*2, points[block.x*2, block.y*2], block.x*2);
    5.             p[1] = new Vector3(block.y*2, points[block.x*2+1, block.y*2], block.x*2+1);
    6.             p[2] = new Vector3(block.y*2, points[block.x*2+2, block.y*2], block.x*2+2);
    7.  
    8.             p[3] = new Vector3(block.y*2+1, points[block.x*2, block.y*2+1], block.x*2);
    9.             p[4] = new Vector3(block.y*2+1, points[block.x*2+1, block.y*2+1], block.x*2+1);
    10.             p[5] = new Vector3(block.y*2+1, points[block.x*2+2, block.y*2+1], block.x*2+2);
    11.  
    12.             p[6] = new Vector3(block.y*2+2, points[block.x*2, block.y*2+2], block.x*2);
    13.             p[7] = new Vector3(block.y*2+2, points[block.x*2+1, block.y*2+2], block.x*2+1);
    14.             p[8] = new Vector3(block.y*2+2, points[block.x*2+2, block.y*2+2], block.x*2+2);
    15.  
    16.             Vector2[] puv = new Vector2[9];
    17.  
    18.             puv[0] = new Vector2(0,0);
    19.             puv[1] = new Vector2(.5f,0);
    20.             puv[2] = new Vector2(1,0);
    21.  
    22.             puv[3] = new Vector2(0,.5f);
    23.             puv[4] = new Vector2(.5f,.5f);
    24.             puv[5] = new Vector2(1,.5f);
    25.  
    26.             puv[6] = new Vector2(0,1);
    27.             puv[7] = new Vector2(.5f,1);
    28.             puv[8] = new Vector2(1,1);
    29.  
    30.  
    31.             Vector3[] verts = new Vector3[]{
    32.  
    33.                 p[4],p[0],p[1],
    34.                 p[4],p[1],p[2],
    35.                 p[4],p[2],p[5],
    36.                 p[4],p[5],p[8],
    37.                 p[4],p[8],p[7],
    38.                 p[4],p[7],p[6],
    39.                 p[4],p[6],p[3],
    40.                 p[4],p[3],p[0]
    41.  
    42.             };
    43.  
    44.  
    45.  
    46.             int[] tris = new int[]{
    47.  
    48.                 0 ,1, 2 ,
    49.                 3 ,4, 5 ,
    50.                 6 ,7 ,8 ,
    51.                 9 ,10,11,
    52.                 12,13,14,
    53.                 15,16,17,
    54.                 18,19,20,
    55.                 21,22,23
    56.  
    57.             };
    58.  
    59.  
    60.  
    61.             Vector2[] uvs = new Vector2[]{
    62.  
    63.                 puv[4],puv[0],puv[1],
    64.                 puv[4],puv[1],puv[2],
    65.                 puv[4],puv[2],puv[5],
    66.                 puv[4],puv[5],puv[8],
    67.                 puv[4],puv[8],puv[7],
    68.                 puv[4],puv[7],puv[6],
    69.                 puv[4],puv[6],puv[3],
    70.                 puv[4],puv[3],puv[0]
    71.  
    72.             };
    73.  
    74.  
    75.             block.mc.sharedMesh.vertices = verts;
    76.             block.mc.sharedMesh.triangles = tris;
    77.             block.mc.sharedMesh.uv = uvs;
    78.  
    79.             block.mc.sharedMesh.RecalculateNormals();
    80.             block.mc.sharedMesh.RecalculateBounds();
    81.             block.mc.sharedMesh.Optimize();
    82.  
    83.  
     
  2. maikkel

    maikkel

    Joined:
    Sep 24, 2010
    Posts:
    20
    Never mind, it was a stupid mistake by me, i assigned a new Mesh() in the block code itself (Awake), which messed things up (and caused a memory leak). Just wasted a Weekend of experimenting and debugging on this a dumb mistake... ;)