Hello , I am writing an editor script that finds all the verts in a mesh, calculate their distance from the object the mesh is on then determine which verts on the x, y and z axis are the farthest away from the objects transform.positions x, y and z axis respectively.
In the code below ,Right after I have calculated the distance of all the verts of the mesh from the object with the mesh I am having trouble finding the farthest verts on the x ,y and z axis and assigning them to an array.
what would be the best way to get this done?
any help is appreciated .
private Vector3 MeshPosition;
private Vector3[] Verts, Farthest;
private Vector3 VertPosition()
{
Verts = Object_With_Mesh.GetComponent<MeshFilter>().sharedMesh.vertices;
if (MeshPosition == new Vector3())
{
try
{
MeshPosition = Object_With_Mesh.GetComponent<Transform>().transform.position;
}
catch
{
}
}
int VertCountPosition = 0;
foreach (Vector3 Vert in Verts)
{
float distance = Mathf.Infinity;
Vector3 diff = Vert - MeshPosition;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
Farthest[VertCountPosition] = Vert;
VertCountPosition += 1;
Debug.Log(Farthest[VertCountPosition]);
distance = curDistance;
}
}
return Farthest[VertCountPosition];
}