- Home /
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.
using UnityEngine;
public class CircleMesh : MonoBehaviour
{
Mesh mesh;
public PolygonCollider2D polygonCollider;
Vector3[] vertPos;
Vector2[] vert2dPos;
public int vertecies = 20;
public float radiusA = 20;
public float radiusB = 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;
vertPos = CalculateVerticies(vertecies, radiusA, radiusB);
for (int i = 0, j = 1; i <= triangle.Length - 3; i += 3)
{
triangle[i] = 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[i] = vertPos[i + 1];
polygonCollider.SetPath(i, vert2dPos);
}
for (int i = 0; i < vert2dPos.Length; i++)
{
Debug.Log(vert2dPos[i]);
}
mesh.vertices = vertPos;
mesh.triangles = triangle;
// polygonCollider.points = vert2d;
}
Vector3[] CalculateVerticies(int corners, float radiusA, float radiusB)
{
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........?
Answer by privatecontractor · Jan 30 at 07:49 PM
Hi @cbetsikos,
Code and result look awesome, did you set Continuous collision detection on Rigidbody component atached to player?
Answer by cbetsikos · Jan 30 at 08:39 PM
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;
public float radiusA = 20;
public float radiusB = 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;
vertPos = CalculateVerticies(vertecies, radiusA, radiusB);
for (int i = 0, j = 1; i <= triangle.Length - 3; i += 3)
{
triangle[i] = 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[i] = vertPos[i + 1];
//polygonCollider.SetPath(i, vert2dPos);
}
for (int i = 0; i < vert2dPos.Length; i++)
{
Debug.Log(vert2dPos[i]);
}
mesh.vertices = vertPos;
mesh.triangles = triangle;
polygonCollider.points = vert2dPos;
}
Vector3[] CalculateVerticies(int corners, float radiusA, float radiusB)
{
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;
}
}
Answer by Madisen54 · Feb 03 at 05:46 PM
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