Detect closed loop in TrailRenderer and get gameObjects inside

I am trying to create a game where the player can draw custom shapes to surround enemies to kill them. I am currently using TrailRenderer to show the shapes drawn but am unable to think of a way to detect that:

  1. the trail forms a closed loop
  2. get the gameObjects inside said closed loop

Hey, you’re probably not active anymore… But did you ever find a solution for this?

Here are the steps I would try:

  1. Use TrailRenderer.GetPositions to get a list of points

  2. Use the last point in that array as your test point

  3. Check the distance from your Test point(2) to all other points on the array(1) starting with the first point (and possibly ignoring that last 3-4 points)

  4. If the distance is within some tolerance (usually TrailRenderer.minVertexDistance) you have found the start of your closed shape. Make an array from this point, to the point before the Test Point (2), you may need to add the start point to the end of this array

  5. If using the Physics2D system Create/Update a PolygonCollider2D with points from (4) and use the Overlaps method, otherwise you will need to test all your selectable gameobjects to see if their center point is within the polygon defined by the points in Step 4

  6. Here is one resource for a Point in Polygon algorithm.