How can I calculate a radius based on an array of transforms? I’m setting some patrol waypoint transforms manually and I’d like to calculate a radius that encompasses every transform, so if my enemy leaves this radius during a chase, he returns to patrolling.
You want a circle that contains all the points? Is that what you’re asking? Does the center of the circle have to be in a specific place or no?
If you already have a center for the circle in mind, just calculate the distance from that center to every point. Your radius can be the same as the greatest distance among those distances.
If you don’t have a center in mind, you can calculate the centroid (geometric mean) point of all your points. This is as simple as adding all the vectors together and dividing by the number of points. Once you have the centroid, you can then calculate the furthest point among your points from that centroid, and use the distance from the centroid to that point as the radius.
The center would by the middle point of all of the transforms. Would the centroid/radius be stored as a vector2?
If you only care about 2D yes you can store it as a Vector2. If you want to
If you care about this you could create a GameObject with a trigger CircleCollider2D or a SphereCollider at the centroid location and give it the radius as calculated. Use that trigger collider to detect entrance/exit of that radius by your enemy.
I was considering using a distance from the center of the centroid/radius as a possibility, since that would be perfectly the center of all of the transforms. Perhaps it might be simpler to calculate the center-point of all transforms in the array?
Yes, the center point of all the transforms would be the centroid.
Would this be a foreach or forloop? I’m just having a bit of trouble getting started with the loop to interate through the transforms array.
You can do either one. It’s a pretty simple calculation:
// Assuming you have an array of transforms...
Transform[] myTransforms;
Vector3 centroid = Vector3.zero
// Add all the points together
foreach (var t in myTransforms) {
centroid += t.position;
}
// Divide by the number of points.
centroid /= (float)myTransforms.Count;
Thank you, I’ll try that! Does this produce a mean or average? I can’t recall exactly but I think one is influenced by a waypoint that might be a lot further away than the others (not that it would happen really for patrol waypoints, but I’m interested in the difference).
The mean is a type of average.
This produces the centroid position of all the positions in the array. It’s like the “average” position of all the given positions. If all of the positions were equal mass objects, it would be the center of mass of all the points. Also known as the barycentre or arithmetic mean.
https://en.wikipedia.org/wiki/Centroid#Of_a_finite_set_of_points
Should I use centroid /= (float)myTransforms.Length;
instead of centroid /= (float)myTransforms.Count;
?
Sorry, yes my bad. Length is for arrays. Count is for Lists. Obviously the intent was just the number of elements in the array.