I have put together this script which is supposed to remove a triangle from a sphere when collided with. It works intermittently. Sometimes when a triangle/face of a sphere is collided with, the triangle does not get removed until I hit some different part of the triangle.
The sphere has a Mesh Collider with Convex unchecked.
The shot object has a sphere collider with a rigidbody. The collision detection is on Discrete.
Here’s my code:
void OnCollisionEnter(Collision collisionInfo)
{
var mesh = gameObject.GetComponent<MeshFilter>().mesh;
MeshCollider tmpCol = gameObject.GetComponent<MeshCollider>();
int[] triangles = mesh.triangles;
for (var i = 0; i < collisionInfo.contacts.Length; i++)
{
RaycastHit hit;
Collider col = GetComponent<Collider>();
col.Raycast(new Ray(collisionInfo.contacts[i].point, collisionInfo.contacts[i].normal), out hit, collisionInfo.contacts[i].point.magnitude);
if (hit.collider != null && hit.triangleIndex != -1)
{
triangles = removeTriangle(hit.triangleIndex, triangles);
}
}
mesh.triangles = triangles;
tmpCol.sharedMesh = mesh;
}
private int[] removeTriangle(int triangle, int[] tris)
{
for (var i = triangle * 3; i < tris.Length - 3; ++i)
{
if (tris[i] == -1) break;
tris[i] = tris[i + 3];
}
return tris;
}