I’m trying to create procedural cube with mesh collider attached to it, but i can’t get it to work.
The problem is that only top face of the cube collides with other objects.
function Start(){
cube();
}
function cube()
{
var verts : Array =
[
Vector3(0,0,0), // front bottom left
Vector3(0,50,0), // front top left
Vector3(50,50,0), // front top right
Vector3(50,0,0), // front bottom right
Vector3(0,0,50), // back bottom left
Vector3(0,50,50), // back top left
Vector3(50,50,50), // back top right
Vector3(50,0,50) // back bottom right
];
var normals : Array = new Array();
var uvs : Array = new Array();
var tri : Array = new Array();
var mf : MeshFilter = GetComponent(MeshFilter);
// Normals
for(var e=0; e<verts.length; e++){
normals[e] = transform.right;
}
// UVs
for(var i=0; i<verts.length; i++){
uvs[i] = Vector2(verts[i].x, verts[i].z);
}
// Triangles
// Front
tri[0] = 0;
tri[1] = 1;
tri[2] = 2;
tri[3] = 0;
tri[4] = 2;
tri[5] = 3;
// Back
tri[6] = 4;
tri[7] = 5;
tri[8] = 6;
tri[9] = 4;
tri[10] = 6;
tri[11] = 7;
// Top
tri[12] = 1;
tri[13] = 5;
tri[14] = 6;
tri[15] = 1;
tri[16] = 6;
tri[17] = 2;
// Bottom
tri[18] = 0;
tri[19] = 4;
tri[20] = 7;
tri[21] = 0;
tri[22] = 7;
tri[23] = 3;
// Left
tri[24] = 0;
tri[25] = 1;
tri[26] = 5;
tri[27] = 0;
tri[28] = 5;
tri[29] = 4;
// Right
tri[30] = 3;
tri[31] = 2;
tri[32] = 6;
tri[33] = 3;
tri[34] = 6;
tri[35] = 7;
mesh = new Mesh();
mesh.vertices = verts;
mesh.triangles = tri;
mesh.uv = uvs;
mesh.normals = normals;
mf.mesh = mesh;
GetComponent(MeshCollider).mesh = mesh;
}