I’m wondering if I could get some help with my collisions, see I make a mesh dynamically this is then used in a mesh collider trigger, but it doesn’t work! Grrr, I have no idea it just doesn’t work in the slightest… I have absolutely no idea why, it is so rage inducing!
I’ve tried adding vector3.zero to the transform.position, still doesn’t work. I know the collider fills the whole area as i’ve rendered it.
When I set it to convex it works perfectly but that is no good as there are holes in the mesh that I need.
What are you triggering it against? If the other collider is also a Mesh Collider, they wont trigger unless they are convex
No, the other colliders are character controllers.
Any one?
This is the function that handles the collider creation:
Mesh CreateMesh()
{
// convert polygon to triangles
Vector3[] poly = linePoints.ToArray();
Triangulator triangulator = new Triangulator(poly);
int[] tris = triangulator.Triangulate();
Mesh m = new Mesh();
Vector3[] vertices = new Vector3[poly.Length*2];
for(int i=0;i<poly.Length;i++)
{
vertices[i].z = poly[i].z;
vertices[i].x = poly[i].x;
vertices[i].y = -10; // front vertex
vertices[i+poly.Length].z = poly[i].z;
vertices[i+poly.Length].x = poly[i].x;
vertices[i+poly.Length].y = 10; // back vertex
}
int[] triangles = new int[tris.Length*2+poly.Length*6];
int count_tris = 0;
for(int i=0;i<tris.Length;i+=3)
{
triangles[i] = tris[i];
triangles[i+1] = tris[i+1];
triangles[i+2] = tris[i+2];
} // front vertices
count_tris+=tris.Length;
for(int i=0;i<tris.Length;i+=3)
{
triangles[count_tris+i] = tris[i+2]+poly.Length;
triangles[count_tris+i+1] = tris[i+1]+poly.Length;
triangles[count_tris+i+2] = tris[i]+poly.Length;
} // back vertices
count_tris+=tris.Length;
for(int i=0;i<poly.Length;i++)
{
// triangles around the perimeter of the object
int n = (i+1)%poly.Length;
triangles[count_tris] = i;
triangles[count_tris+1] = i + poly.Length;
triangles[count_tris+2] = n;
triangles[count_tris+3] = n;
triangles[count_tris+4] = n + poly.Length;
triangles[count_tris+5] = i + poly.Length;
count_tris += 6;
}
m.vertices = vertices;
m.triangles = triangles;
m.RecalculateNormals();
m.RecalculateBounds();
m.Optimize();
meshCollider = sellectionHolder.AddComponent(typeof(MeshCollider)) as MeshCollider;
meshCollider.sharedMesh = m;
meshCollider.isTrigger = true;
meshCollider.convex = true;
sellectionHolder.tag = "Sellection";
rigid = sellectionHolder.AddComponent(typeof(Rigidbody)) as Rigidbody;
rigid.isKinematic = true;
sellectionHolder.transform.position += Vector3.zero;
return m;
}
Have you made sure your triangles are wound in the correct order?
I think so, is there any way to test this?
You could add a MeshFilter and MeshRenderer to the game object and use those to render the mesh. Use a material that has back face culling on and it will be fairly clear if things are wrong.
Tried to set normals manually?
Thanks, that seems to be it my normals are all messed up. I can’t find any information on how to manually modify these though? How do you usually modify this or calculate the direction?
Well when i needed to calculate them manually it was for a voxel engine (poxel) where knew which direction my normals should be in.
But it it would be possible to calculate a normal based on the cross product of two vectors gained from a triangles three vertices. But i am sure there is a smarter way to do this, just can come up with it right now.
Normals have nothing to do with collision detection. If triangles aren’t rendering then it means they are being culled because their vertices aren’t wound in the correct order. Typically you can determine this by looking at the z component of the cross product between the points of the triangle. If z is negative then the triangle isn’t wound correctly.
Funny how our collision detection suddenly worked when we fixed our normals then. Or how you can go through one side of a primitive plane object but not the other (talking normals for the mesh used for the mesh collider ofcause)
Funny how my custom NavMesh works fine without any normals at all. Also - a primitive plane is still subject to triangle winding order.
EDIT: Thinking about it - that might be an interesting point. Raycasting against a collider versus other kind of collision detection. Hmmmm…
Winding order doesn’t really matter if you set your normals yourself
Yes it does. The engine will still cull a triangle due to winding order even if the normals are pointed right at you.