Compound Colliders for dynamically created shapes

Hi,

First of all I’d like to thank any who do manage to take the time to read this. I’m pretty new to game development so this may be a relatively simple set of questions. My aim is to allow players to essential draw a 2D polygon with a rigidbody and a set of colliders. Basically, it should interact with the world around it as a physics object. Currently what I have done is a 2.5D approach where I’m creating a mesh with the given points, all of which are at the same point in the Z axis. The 2D mesh works fine, however here’s my first question, is the 2.5D approach correct, or is there a way to allow players to dynamically draw 2D sprites? Secondly, and my this is where my issue lies, as the mesh can be either convex or concave depending on how its drawn I don’t think I can use a mesh collider (from my understanding they only calculate collisions with convex meshes?). I have begun the process of wrapping the shape in a series of box colliders. I have a “DrawnPoly” class which handles the creation of the mesh and colliders. I also have an empty prefab of a drawn poly which I instantiate after performing the desired input (making a closed loop of vertices). Below is a code snippet im using to create, orient, scale and size my colliders:

private void createColliders(Vector3[] points)
    {
        // Variables used for future calculations
        int nextPoint = 1;
        float distance = 0f;
        Vector3 colliderMidpoint = new Vector3 ();
        float angle = 0f;

        // Clear previous collider list
        colliders.Clear ();

        // Calculate the centre of mass, used for moving edges inwards to account for collider width)
        Vector3 midPosition = MathHelper.calculateMidpoint (points);

        //Create colliders for each edge
        for (int i = 0; i < points.Length; i++)
        {
            //Connect to first point
            if (i == points.Length-1) {
                nextPoint = 0;
            }
            //Calculate Angle between points
            angle = Mathf.Atan2 (points [nextPoint].y - points [i].y, points [nextPoint].x - points [i].x) * 180 / Mathf.PI;

            //Calculate Distance to next point (minus collider width
            distance = Vector3.Distance(points[i],points[nextPoint])-COLLIDER_WIDTH;

            //Calculate where to place the midpoint of the collider
            colliderMidpoint = (points [i] + points [nextPoint]) / 2;
            colliderMidpoint = Vector3.MoveTowards (colliderMidpoint, midPosition, COLLIDER_WIDTH);

            //Attatch, Size and Rotate collider
            GameObject colliderHolder = new GameObject ();
            colliderHolder.name = "Collider-" + i;
            BoxCollider tempCollider = colliderHolder.AddComponent<BoxCollider> ();
            colliderHolder.transform.parent = gameObject.transform;
            tempCollider.size = new Vector3(distance,COLLIDER_WIDTH,COLLIDER_DEPTH);
            tempCollider.transform.localPosition = colliderMidpoint;
            tempCollider.transform.Rotate(new Vector3(0f,0f,angle));
            colliders.Add (colliderHolder);

            nextPoint++;
        }
    }

And here is how I call the above method:

Instantiate (prefab, MathHelper.calculateMidpoint (points), Quaternion.identity);
DrawnPolygon newPolygon = prefab.GetComponentInChildren<DrawnPolygon> ();
newPolygon.createPolygon (points);

The createPolygon method call just creates the mesh and calls the first code snippet. My issue is however, that I am unable to set the parent for the transform of a prefab. How do I go about doing what I’m trying to achieve? I have no idea how to do CompoundColliders, but I do find this fascinating. If anyone can provide any input I would be extremely greatful.

Thanks,
Nic

For anyone interested, I realized the issue. I’ve changed it so that instead of an object trying to draw itself, there is a polygon drawer which draws the poly and treats them as their own entity. Basically, changed it to a spawner :).