How do I check for whether a raycast is hitting a surface normal or if it's hitting the inside of a collider?

8 hours ago 1
ARTICLE AD BOX

I'm specifically using Physics2D raycasts and I want to check for this in order to prevent multiple collisions occurring around the same surface (as the raycast hit point overshoots slightly due to floating-point errors and causes the next raycast check to start from within the collider).

My initial idea was to use the quirk in RaycastHit2D.normal that returns the opposite direction of the raycast when the hit is from within the collider and check for equivalencies, but not only does this allow for the small chance to not register perfectly direct collisions to the normal, but it's also not accurate beyond the 2nd decimal place, meaning I'd have to round the values for it to work and make direct collisions more likely to break my physics system.

What's a better way to check for this? If only the RaycastHit2D.normal quirk returned the same raycast direction instead of the opposite so that I could make the system only check for angles within 90 degrees of the raycast direction and inverse normal direction, but alas. I guess I could also subtract an error from the raycast hit point to push it outside the collider, but I'd rather something more deterministic. Any ideas?

Read Entire Article