# Modify this script for PolygonCollider2D

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;

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)*

•  		{*

•  		}*

•  		else*

•  		{*

•  		}*

_ 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)*

•  	{*

•  	}*

•  	else*

•  	{*

•  	}*

_ 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;*

•  }*

the best way to do these things is first understand the operation of the script and the goal of the script.

in this case your going from 3d collisions to 2d collisions. which means what? No Z axis, so you would rewrite the code so that it no longer takes the z axis into account. but meshes are always in 3d so be wary.

try looking at the differences of what the script is currently doing and what you want it to be doing. for example character turns blue, i want it red, so i find whatever is making him blue and change it so it makes him red.

some key differcnes you should make note of

Hope it helps and good luck let me know if you need anymore help