# How can I find the center of two Vector3's?

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.

The 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;
}
``````

You 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.

public 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;
}