I generated a procedural mesh using marching squares and the MeshFilter
component (it's 2 dimensional). Now I got the point that I need do collision detection with the character (a sprite with a box collider).
I tried simply converting the mesh's vertices to Vector2s and putting it inside the collider the following way:
PolygonCollider2D collider = chunk.gameObject.AddComponent<PolygonCollider2D>();
List<Vector2> points = new List<Vector2>();
foreach (Vector2 vec in vertices)
{
points.Add(vec);
}
collider.points = points.ToArray();
This almost worked, but it had some flaws in the collider:
Then I tried to use an EdgeCollider
instead, but this made the character jitter. Lastly I tried to set each shape coming out of the marchingsquares algorithm be a separate path, but this was somehow very slow.
Is there any ways to create a good collider for a procedural surface?
Edit: Edge collider approach as requested (I got a bit further with it in the mean time):
So, to implement the edge collider, I changed my marching squares algorithm a bit, so it can output pure lines instead of actual triangles. The problem with this was that at some points, the character just randomly goes into the collider:
EdgeCollider
approach is probably your best bet for meshes that can be concave like this. Can you show us what your edge-based implementation looked like? We might be able to help you find & eliminate the character jitter bug that you describe. \$\endgroup\$