Is there a way to find the center of two (or more) Vector3's? I know the Unity Editor does it automatically; when you select two or more objects, the handles move to the center of the group. How do I implement this myself?
Thanks, Elliot Bonneville
Is there a way to find the center of two (or more) Vector3's? I know the Unity Editor does it automatically; when you select two or more objects, the handles move to the center of the group. How do I implement this myself?
Thanks, Elliot Bonneville
Vector3.Lerp(point1.transform.position, point2.transform.position, 0.5f);
The last parameter (0.5) is the desired percent between the 2 points.
This answer is great to find also other positions between the two Vectors. Like 1/4 or 9% from one point to another. Just fix the typos :) "Vector3" (forgot the 'c') and last argument must be float: "0.5f".
– ZielakThe center of n object is also called the centroid or balanced barycenter. You just have to sum all vectors and then divide by the number of vectors.
For example:
public Vector3 CenterOfVectors( Vector3[] vectors )
{
Vector3 sum = Vector3.zero;
if( vectors == null || vectors.Length == 0 )
{
return sum;
}
foreach( Vector3 vec in vectors )
{
sum += vec;
}
return sum/vectors.Length;
}
This is great! Nice and simple. I made a slight adjustment (I prefer Lists), and turned it into an extension method. Thanks for sharing! public static Vector3 CenterOfVectors(this List<Vector3> vectors) { Vector3 sum = Vector3.zero; if (vectors == null || vectors.Count == 0) { return sum; } foreach (Vector3 vec in vectors) { sum += vec; } return sum / vectors.Count; }
– navilnYou can do this by taking the two Vectors and dividing them by two, then add them together. If these objects are in local space meaning placed inside of a parent. Then the results would be different. You would then need to use.
http://unity3d.com/support/documentation/ScriptReference/Transform.TransformPoint.html
ex.
(LeftLegObj.TransformPoint(LeftLegObj.localPosition) * 0.5f + RightLegObj.TransformPoint(RightLegObj.localPosition) * 0.5f);
I solved the problem by getting the transforms of the objects I wanted to find the center of, finding the greatest x, y, and z values, dividing those by 2, and creating a Vector3 out of them.
That gives me the exact center of the transforms, but if you have a better idea, feel free to let me know.
Apparently this approach isn't working for some reason. Well, back to the ol' drawing board, I guess.
– e-bonnevillepublic Transform point1, point2, point3;
void Update(){
point3.position = (point1.position + point2.position) / 2;
}
Vector3 GetCenter(Component components)
{
if (components != null && components.Length > 0)
{
Vector3 min = components[0].transform.position;
Vector3 max = min;
foreach (var comp in components)
{
min = Vector3.Min(min, comp.transform.position);
max = Vector3.Max(max, comp.transform.position);
}
return min + ((max - min) / 2);
}
return Vector3.zero;
}
http://answers.unity3d.com/questions/212192/which-is-the-center-between-4-points-mathematicall.html
– Jessy