Get which edge a raycast 2d hit on a polygon collider

So I am writing a Sonic engine over here in 2D, and the way angles are calculated in the Mega Drive games is pretty dated (each object has its own angle value). So to counteract that, I tried to automate that by calculating the angle for each edge in polygon colliders. That is fun and dandy, however whenever one of the ground raycasts hit the polygon, I need to tell which edge of that polygon it hit. I’ve tried to compare the raycast point to the coordinates of the point that make the edge, but it spits out weird results. Here’s a representation of what I’m trying to do

I’ve tried to wrap my head around this for hours and I can’t seem to get it. Any help would be appreciated!

Why go to all that trouble when the surface normal is returned in the hit itself?

RaycastHit2D.normal

Also, you might want to look into using the EdgeCollider2D which is perfect for creating continuous surfaces where you only care about contact with the surface. It also doesn’t suffer from discontinuities causing collision “hitches” (ghost collisions) between the various sub-shapes produced by things like the PolygonCollider2D. Even better, it’s better for performance.

1 Like