In my game, I have Tiled "rooms" that were imported with Tiled2Unity. All of these rooms have walls that are defined by Polygon Collider 2D objects. In the event of a cave or dungeon, I'd like to implement torches. Currently, I literally randomly place torches in each room, which is pretty good for lighting, but looks ridiculous.
After the room is added, rather than randomly place torches anywhere, I'd like to have them placed along the inside of the collider. Is there a good way to make this happen?
Edit:
One of my first attempts was to get a list of all of the points in all colliders in a given room after it's instantiation. I was starting with something like this.
GameObject _roomGO =
(GameObject)Instantiate(_room.gameObject, new Vector3 (-6.5f, 6.5f, 0f), Quaternion.identity);
List<PolygonCollider2D> _polyList =
_roomGO.GetComponentsInChildren<PolygonCollider2D>().ToList();
However, this only gets the points from the first collider, whereas I need the points for all colliders in the room GameObject. Additionally, I'm not sure how those points are stored in such a way that I could create a List
or Array
of lines, then evaluate those lines to determine which are inside the room (as opposed to along the outside, so nothing in the set [(x,0),(x,-13),(0,y),(13,y)].
Once I had a list of lines that weren't part of that set, I could choose a point on the line and drop in a torch.
Edit2 - images:
Edit 3:
Thank to @SP., I've used some of his code as shown here:
if (_torches)
{
Transform _roomGOLayer = _roomGO.transform;
int numTorch = rndObjects.Next(2,4);
List<PolygonCollider2D> _polyList = _roomGO.GetComponentsInChildren<PolygonCollider2D>().ToList();
foreach (PolygonCollider2D polCol in _polyList)
{
foreach (Vector2 point in polCol.GetPath(0))
{
Debug.Log(point.x +" : "+ point.y);//you will be using these values
GameObject _to = Instantiate(_torch, new Vector2(point.x, point.y), Quaternion.identity) as GameObject;
_to.transform.parent = _roomGOLayer;
_to.transform.position = _to.transform.parent.transform.position + new Vector3 (point.x, point.y, 0f);
}
}
}
The biggest problem I'm having up front is that _polyList is only getting the first collider definition. I think if I could solve that, I'd be in good shape.