Hi, I would like to find an efficient way of finding the extremities of an object from a given point.
Because an image is worth 1000 words, this is what I’m trying to achieve
I already have an idea that would involve raycasting with increments of say 10° and then doing a dichotomy search to approximate the result (with a given angle for error margin)But is there a more elegant, more efficient and more accurate way of doing this?
Note that I’m using trigger colliders, so I can’t use Collider2D.GetContacts
Bounds would probably be your best bet here. Grab the object’s centre point as a Vector3 (or Vector2 for 2D), and add/subtract the extents along whatever axis you need to figure out where the object’s edges are. From there it’s easy subtraction to get position relative to other points.
For example, that green line from A to your cube? Essentially, a line drawn from point A to Bounds.center.x - Bounds.extents.x. You may have to play with it a bit to handle rotation or irregular shapes.
Collider2D also uses Bounds, which might help you.
I'll have to play around with that, however I'm not quite sure what happens in the case of a circle collider, or an edge collider. I'll do some testing on more complex shapes and see if that's indeed what I need.
I'll have to play around with that, however I'm not quite sure what happens in the case of a circle collider, or an edge collider. I'll do some testing on more complex shapes and see if that's indeed what I need.
– jeango