- Home /
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:
the trail forms a closed loop
get the gameObjects inside said closed loop
Answer by Foreverlimbo · Jan 26, 2021 at 01:43 PM
Hey, you're probably not active anymore.. But did you ever find a solution for this?
Answer by GameDevMig · Jan 26, 2021 at 08:57 PM
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.
Your answer
Follow this Question
Related Questions
How to force a trail renderer to re-render in the same frame? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers