0
\$\begingroup\$

I'm in the process of incorporating Tiled2Unity prefabs into my code. Their exporter creates a collision object (shown below) that amalgamates the collider of each tile into a single Polygon Collider 2D made up of multiple "subcollider" paths, shown below as green polygons.

Unity window with Collider and Paths

What I'm trying to do is to adapt my old corner detection code to work with the new collider, but when OnTriggerStay2D(Collider2D other) is triggered, I'm not provided with the specific path I've collided with. In the picture, each green polygon is a path, and together they make up the Collider2D (other) provided in the argument.

Is there any way to find out which path I've collided with, without looping through each of the 1000ish points every time I want to detect a nearby corner? So far I am only able to discover via events that I've collided somewhere in the ENTIRE collection of green polygonal paths.

Thanks!

\$\endgroup\$
  • \$\begingroup\$ Since your map is a grid, it seems you should be able to take the collision point (or location of the trigger) and convert it to a grid coordinate to identify which tile(s) you are contacting. Does this get you the info you need? If not, can you edit your question to include more information about what kinds of detection you ultimately want to perform? \$\endgroup\$ – DMGregory May 3 '16 at 2:43
  • \$\begingroup\$ That's just the problem, the most specific information about my Polygon2DCollider that the collision event OnTriggerStay2D(Collider2D other) seems to provide is the collider itself: not the collision point or points, not the individual path (sub-polygon) that was collided with, but the whole collider in its entirety. Note how the collider is partitioned into many green sub-polygons. I would like to know which sub-polygon I collide with, rather than be given the ENTIRE sub-polygon collection. I'll edit to make it more clear. \$\endgroup\$ – Stephen Schroeder May 3 '16 at 14:53
0
\$\begingroup\$

When you have a collision, do a CircleCast (or BoxCast, depending on the shape of the collider of the colliding object) from the center of the colliding object with a size equal to its own collider, a distance of 0 and a layerMask setting which only covers the polygon collider.

The resulting RaycastHit2d object will tell you the closest point of the polygon collider.

It doesn't tell you the edge you collided with, but it tells you its surface normal. Now that you know the orientation you can check one unit left and right of the collision point if these points are also inside the PolygonCollider (PolygonCollider.OverlapPoint). When they are not, you are at a corner.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I assume you mean 1 unit left and right relative to the direction of the raycast? This is a great solution and super close to detecting the corner. I think if I shoot another ray from the left/right point outside the collider towards the raycast hit, I should get the corner. Thanks! \$\endgroup\$ – Stephen Schroeder May 3 '16 at 16:53
  • \$\begingroup\$ @StephenSchroeder No, I mean left and right relative to the direction of the direction of the surface normal. The surface normal is perpendicular to the collider edge at the position the raycast hit it. \$\endgroup\$ – Philipp May 3 '16 at 17:00
  • \$\begingroup\$ Ah of course. I should only be caring about horizontal raycasts and perpendicular corners anyway, but your way is more robust. \$\endgroup\$ – Stephen Schroeder May 3 '16 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.