# Polygon Collider 2d doesnt work properly

Hello (and sorry for my bad English),
I want to make a game with 2d Planets. I have managed to create a Mesh, but the Polygon Collider 2d doesnt work properly. Most sides of the Planet collision are working, but on a few sides the Player is falling through the Planet.
[191885-unityhelp.png*_|191885]

``````using UnityEngine;

public class CircleMesh : MonoBehaviour

{
Mesh mesh;
public PolygonCollider2D polygonCollider;
Vector3[] vertPos;
Vector2[] vert2dPos;
public int vertecies = 20;

Vector3[] verticies;
int[] triangle;
private void Start()
{
vertPos = new Vector3[vertecies + 1];
vert2dPos = new Vector2[vertecies];
triangle = new int[vertecies * 3];
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;

for (int i = 0, j = 1; i <= triangle.Length - 3; i += 3)
{
triangle *= j;*
``````

triangle[i + 1] = 0;
j++;
if (i == triangle.Length - 3)
{
triangle[i + 2] = 1;
}
else
{
triangle[i + 2] = j;
}
}
polygonCollider.pathCount = vertecies;
for (int i = 0; i < vert2dPos.Length; i++)
{
vert2dPos = vertPos[i + 1];
polygonCollider.SetPath(i, vert2dPos);
}
for (int i = 0; i < vert2dPos.Length; i++)
{
Debug.Log(vert2dPos*);*

}

mesh.vertices = vertPos;
mesh.triangles = triangle;

// polygonCollider.points = vert2d;

}

{
float angle = (2 * Mathf.PI) / corners;
Vector3[] verticies = new Vector3[corners + 1];
float currentAngle = 0;
verticies[0] = new Vector3(0, 0, 0);
for (int i = 0; i < corners; i++)
{
currentAngle = currentAngle + angle;
float xPos = Mathf.Cos(currentAngle) * radiusA * Random.Range(1, 1.2f);
float yPos = Mathf.Sin(currentAngle) * radiusB * Random.Range(1, 1.2f);
Vector3 vertice = new Vector3(xPos, yPos, 0);
verticies[i + 1] = vertice;
}
return verticies;

}
}

*
Can someone help me…?_

Hi @cbetsikos,

Code and result look awesome, did you set Continuous collision detection on Rigidbody component atached to player?

Hello @privatecontractor,
thank you. Yes I have set Continuous collision detection on the Rigidbody.
I think the problem is in the Code, because when I use polygon.points instead of polygon.Setpath
the collision works fine. But the collider doesnt fit perfectly to the mesh.
Here is what I mean:

and here the Code:

``````using UnityEngine;

public class CircleMesh : MonoBehaviour

{
Mesh mesh;
public PolygonCollider2D polygonCollider;
Vector3[] vertPos;
Vector2[] vert2dPos;
public int vertecies = 20;

Vector3[] verticies;
int[] triangle;
private void Start()
{
vertPos = new Vector3[vertecies + 1];
vert2dPos = new Vector2[vertecies];
triangle = new int[vertecies * 3];
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;

for (int i = 0, j = 1; i <= triangle.Length - 3; i += 3)
{
triangle *= j;*
``````

triangle[i + 1] = 0;
j++;
if (i == triangle.Length - 3)
{
triangle[i + 2] = 1;
}
else
{
triangle[i + 2] = j;
}
}
//polygonCollider.pathCount = vertecies;
for (int i = 0; i < vert2dPos.Length; i++)
{
vert2dPos = vertPos[i + 1];
//polygonCollider.SetPath(i, vert2dPos);
}
for (int i = 0; i < vert2dPos.Length; i++)
{
Debug.Log(vert2dPos*);*

}

mesh.vertices = vertPos;
mesh.triangles = triangle;

polygonCollider.points = vert2dPos;

}

{
float angle = (2 * Mathf.PI) / corners;
Vector3[] verticies = new Vector3[corners + 1];
float currentAngle = 0;
verticies[0] = new Vector3(0, 0, 0);
for (int i = 0; i < corners; i++)
{
currentAngle = currentAngle + angle;
float xPos = Mathf.Cos(currentAngle) * radiusA * Random.Range(1, 1.5f);
float yPos = Mathf.Sin(currentAngle) * radiusB * Random.Range(1, 1.5f);
Vector3 vertice = new Vector3(xPos, yPos, 0);
verticies[i + 1] = vertice;
}
return verticies;

}
}

you should check Rigidbody Component that attached to objects because Collision need physics component(if game was 2d you should use RigidBody2D) and disable isKinematic because Collision don’t work