So it’s fairly evident that 2d colliders are pretty messed up. Half the time their callbacks don’t work, etc.
I am attempting to work around this by using 3d colliders. However i am having trouble. I want to make a 2d game with a simple mesh and a rigidbody with a sphere collider that bounces off the mesh. I think i am doing everything properly but the sphere collider does not collide with my mesh.
Here is the relevant code:
// this is the mesh generation code from my Terrain GameObject.
private void CreateMesh()
{
int numVerts = ((int) (dimensions.x / triWidth)) * 2;
int[] tris = new int[3 * (numVerts - 2)];
Vector3[] verts = new Vector3[numVerts];
Vector3[] normals = new Vector3[numVerts];
Vector2[] uvs = new Vector2[numVerts];
float curY = meshPos.y;
for (int i = 0; i < verts.Length; i += 2)
{
if (i + 1 < verts.Length - 1)
{
float curX = ((i / 2) * triWidth);
if (i > 1)
{
curX = curX + Random.Range(-deformX, deformX);
curY = curY + Random.Range(-deformY, deformY);
}
verts[i] = new Vector3(curX, curY, 0);
verts[i + 1] = new Vector3(curX, mins.y, 0);
}
else
{
verts[i] = new Vector3(dimensions.x, curY, 0);
verts[i + 1] = new Vector3(dimensions.x, mins.y, 0);
}
}
meshPos.y = curY;
for (int i = 0; i < numVerts; i++)
{
if ((i % 2) == 0)
{
uvs[i] = new Vector2(1, 1);
normals[i] = Vector3.up;
}
else
{
uvs[i] = new Vector2(0, 0);
normals[i] = Vector3.down;
}
}
int pos = 0;
for (int i = 0; i < tris.Length; i += 3)
{
tris[i] = pos;
tris[i + 1] = pos + 1;
tris[i + 2] = pos + 2;
pos++;
}
mesh = new Mesh();
mesh.vertices = verts;
mesh.uv = uvs;
mesh.normals = normals;
mesh.triangles = tris;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
renderer = gameObject.AddComponent("MeshRenderer") as MeshRenderer;
filter = gameObject.AddComponent("MeshFilter") as MeshFilter;
collider = gameObject.AddComponent("MeshCollider") as MeshCollider;
renderer.material = materials[0];
filter.mesh = mesh;
collider.sharedMesh = mesh;
}
I am using AddComponent to create the mesh stuff since this is a prefab and i am instantiating multiple prefabs.
I am testing by putting a sphere collider with a rigid body on top of the mesh but it just falls through the mesh. Any ideas?