Hi guys, I am implementing a Tile Map, from Tiled Map generator using XuniTMX2D from here Bitbucket, i managed to modify the scripts to implement boxCollider2D instead of boxCollider but i have tried to change the method of generating Polygon collider mesh into PolygonCollider2D and It has not work, this is the original code, could someone help me transforming it into PolygonCollider2D? thanks in advance.
/// <summary>
/// Generate a Polygon collider mesh
/// </summary>
/// <param name="obj">Object which properties will be used to generate this collider.</param>
/// <param name="zDepth">Z Depth of the collider.</param>
/// <param name="colliderWidth">Width of the collider.</param>
/// <param name="innerCollision">If true, calculate normals facing the center of the collider (inside collisions), else, outside collisions.</param>
/// <returns>Generated Game Object containing the Collider.</returns>
public GameObject GeneratePolygonCollider(MapObject obj, float zDepth = 0, float colliderWidth = 1.0f, bool innerCollision = false)
{
GameObject polygonCollider = new GameObject(obj.Name);
polygonCollider.transform.parent = this.Parent.transform;
Mesh colliderMesh = new Mesh();
colliderMesh.name = "Collider_" + obj.Name;
MeshCollider mc = polygonCollider.AddComponent<MeshCollider>();
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
Vector3 firstPoint = (Vector3)obj.Points[0];
Vector3 secondPoint, firstFront, firstBack, secondFront, secondBack;
for (int i = 1; i < obj.Points.Count; i++)
{
secondPoint = (Vector3)obj.Points*;*
-
firstFront = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth - colliderWidth);* -
firstBack = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth + colliderWidth);* -
secondFront = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth - colliderWidth);* -
secondBack = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth + colliderWidth);* -
if (innerCollision)* -
{* -
vertices.Add(firstBack); // 3* -
vertices.Add(firstFront); // 2* -
vertices.Add(secondBack); // 1* -
vertices.Add(secondFront); // 0* -
}* -
else* -
{* -
vertices.Add(firstFront); // 3* -
vertices.Add(firstBack); // 2* -
vertices.Add(secondFront); // 1* -
vertices.Add(secondBack); // 2* -
}*
_ triangles.Add((i - 1) * 4 + 3);_
_ triangles.Add((i - 1) * 4 + 2);_
_ triangles.Add((i - 1) * 4 + 0);_
_ triangles.Add((i - 1) * 4 + 0);_
_ triangles.Add((i - 1) * 4 + 1);_
_ triangles.Add((i - 1) * 4 + 3);_
-
firstPoint = secondPoint;* -
}* -
// Connect last point with first point* -
secondPoint = (Vector3)obj.Points[0];* -
firstFront = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth - colliderWidth);* -
firstBack = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth + colliderWidth);* -
secondFront = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth - colliderWidth);* -
secondBack = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth + colliderWidth);* -
if (innerCollision)* -
{* -
vertices.Add(firstBack); // 3* -
vertices.Add(firstFront); // 2* -
vertices.Add(secondBack); // 1* -
vertices.Add(secondFront); // 0* -
}* -
else* -
{* -
vertices.Add(firstFront); // 3* -
vertices.Add(firstBack); // 2* -
vertices.Add(secondFront); // 1* -
vertices.Add(secondBack); // 2* -
}*
_ triangles.Add((obj.Points.Count - 1) * 4 + 3);_
_ triangles.Add((obj.Points.Count - 1) * 4 + 2);_
_ triangles.Add((obj.Points.Count - 1) * 4 + 0);_
_ triangles.Add((obj.Points.Count - 1) * 4 + 0);_
_ triangles.Add((obj.Points.Count - 1) * 4 + 1);_
_ triangles.Add((obj.Points.Count - 1) * 4 + 3);_
-
colliderMesh.vertices = vertices.ToArray();* -
colliderMesh.triangles = triangles.ToArray();* -
colliderMesh.RecalculateNormals();* -
mc.sharedMesh = colliderMesh;* -
polygonCollider.isStatic = true;* -
return polygonCollider;* -
}*
No one here is going to do your work for you, we are simply here to help point you in the right direction.
– SildaekarDepends @Sildaekar , people do get bored or feel like helping a bit more sometimes.
– Lo0NuhtiKHe's just asking for help, not to have it done completely for him.
– JasonBriccoI don't want you to do it for me, I just need a bit of help, thanks in advance.
– BanzaiPipeline