When I make circleCast2d intersects with another circle collider, I get a RaycastHit2D.point that doesn't point at the tip of the collision as it states in the documentation. Instead it always points at the centre of the intersected junction or pointC as shown in the figure below.

I checked this numerous times with different values to make sure the error was not from my end.

Is this intended? If so is there another way where I can actually find the closest point from collider(B) to the centre of collider(A) which corresponds to point(D) in the figure?

This is a picture of what looks like:

A = circleCast
B = collided object
C = where RaycastHit2D.point is pointing at
D = where I'd like it to point. Text

  • \$\begingroup\$ Can you show us your CircleCast2D code that produces this result? It looks like you're firing a circle from a position where it already overlaps, so there is no point of collision, and the middle of the overlapping region is the best value the physics engine can provide you in that case. Did you want Physics2D.OverlapCircle, Collider2D.OverlapCollider, or Collider2D.ClosestPoint instead? \$\endgroup\$ – DMGregory Mar 19 at 16:02
  • \$\begingroup\$ @DMGregory thank you Collider2D.ClosestPoint did the exact thing I wanted. for a while, unity kept saying that "collider2d doesn't have a function called ClosestPoint". turns out I was still using 2018 build and they only added it in later updates. again thank you \$\endgroup\$ – alqaadi Mar 20 at 10:02
  • \$\begingroup\$ Want to add your solution as an Answer below? \$\endgroup\$ – DMGregory Mar 20 at 12:03

As @DMGregory pointed out, Collider2D.ClosestPoint was exactly what I was looking.

And the question of why is 'RaycastHit2D.point' misbehaving, I still don't know. but I have a feeling it was because I was treating it like an expanding circle instead of it being static circle collider.

  • \$\begingroup\$ This answer (and your question) would be clearer if you showed a sample of the code you're using, not just the name of the method. I can tell you that RaycastHit2D is not "misbehaving," but you may have been misusing it. Without seeing your code though, we can't help you find the problem in your code and explain what you did wrong. \$\endgroup\$ – DMGregory Mar 20 at 18:43

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.