How to get Vector3.Distance from the perimeter of a circle?
Image here: https://drive.google.com/file/d/1YeJ828svhG2u28-yq3NpR8kxavJgv_-u/view?usp=sharing
My script is to detect the closest solid circle and then interact with it once you reach the transparent area around it. The smallest distance between in an array of distances between the player and the circle will be selected as the closest circle. If you are within the area of effect(transparent circle) you will be pulled towards it. The issue is, since it is using Vector3.Distance to find the center of each circle, if a small circle, like the pink one is closer to the player's position than a larger circle, even though the player is within the area of effect of the orange circle, noting happens. Is there a way that I can find the distance between the player and the perimeter of the area of effect? Using that instead of the center will solve my issue.
Answer by Bunny83 · Jun 03, 2019 at 09:58 PM
Just subtract the radius of the two circles you're testing from the measured distance between their centers. This will give you the distance between the perimeters of the two circles. Note that if the circles overlap you actually get a negative value which does make sense as they are "closer" than touching.
like this? (planet is the object in an array of planets, foreach planet in planets)
(Vector3.Distance(transform.position, planet.transform.position) - (planet.GetComponent().lossyScale.x / 2) < nearestDist)