I have a dynamically generated mesh, of a theoretically arbitrary shape. But it’s definitely convex, not smooth and manifold. There is no guarantee of how much of its bounds are filled

What i want to do is find a vector which sits on top of the mesh. Basically, a line starting from the centre of the mesh and going in the world direction <0,1,0> until it exits the mesh.

Note world direction, this is important. I need to find the top of the mesh right now, however it happens to be rotated. And i’m doing this on dynamic objects which are going to roll/slide around on the ground a fair bit

Well it’s a bit vague what you really want. Having a vector starting at the center and going into world-y-axis doesn’t need to exit the mesh at the top most point. So do you need the top most vertex or the exit point of your “ray” from the origin up?

To find the topmost vertex in world space there are basically two ways:

Use Transform.TransformPoint on each vertex to get the worldspace position and do a max-search similar to what Cherno did.

get the worldspace up orientation into localspace and do some magic there.

Either project each local space point onto the normalized worldspace up vector which you first transform into localspace. You’ll end up with direction vectors which length represent the distance from the origin in world up direction. You can use the sqrMagnitude to find the largest value.

Another way is to use Unity’s Plane struct (which represents a mathematical plane) and use again the up vector transformed into localspace as normal and 0,0,0 as origin. Now you can simply use Plane.GetDistanceToPoint. Just again search for the largest value.

If you want to find the exit point of the vector starting at the origin along the y axis it’s the easiest way to use a MeshCollider and doing a Raycast from outside to the center. You can use Collider.Raycast in this case.

edit
To get the highest vertex you would do something like:

Vector3[] verts = GetComponent<MeshFilter>().sharedMesh.vertices;
Vector3 topVertex = new Vector3(0,float.NegativeInfinity,0);
for(int i = 0; i < verts.Length; i++)
{
Vector3 vert = transform.TransformPoint(verts[i]);
if(vert.y > topVertex.y)
{
topVertex = vert;
}
}

For this you need to convert every vertex into worldspace. The second way, to project the worldspace up vector into local space and use the Plane object would look like this: