How to find the uppermost/lowermost point of a mesh depending on a direction

Hello everyone,

I would like to attach a camera above and behind a thrown object of variable size and variable rotation.

So I think I need to find a topmost vertex of a mesh perpendicular to the thrown direction (which is captured in a rotated transform). And topmost vertex from the direction of the throw. Finally with both of the distances from the gameObject pivot get a coordinate (blue circle) with some additional offset and attach a camera to it.

direction of throw

I have found sort of a solution here:

Vector3 worldUp = transform.InverseTransformDirection(Vector3.up).normalized;
Plane p = new Plane(worldUp, Vector3.zero);
Vector3[] verts = GetComponent<MeshFilter>().sharedMesh.vertices;
Vector3 topVertex = Vector3.zero;
float maxDist = float.NegativeInfinity;
for(int i = 0; i < verts.Length; i++)
{
    float dist = p.GetDistanceToPoint(verts);
    if (dist > maxDist)
    {
        maxDist = dist;
        topVertex = verts;
    }
}
topVertex = transform.TransformPoint( topVertex );

However, I am unfortunately at loss in making it work with the transform’s direction and in gameObject’s local space rather than world space. Is the only solution to clone the mesh rotate it in a way that would get me the desired directions in the world space and then rotate it back to get the point. Or is there a more sophisticated way to do so ?

Thank you for any thoughts !!!

Well, the code gets the worldspace up direction into local space in order to find the furthest vertex inside the local space of the mesh. When you have a different “up” vector, just use that instead of Vector3.up.

From your description it’s not really clear what you actually want since you don’t seem to be clear about that yourself :slight_smile: There are many things unclear. You said the object is thrown, does it actually rotate? Should the camera follow the object? Should the camera be “attached” to the object (so when the object rotates, the camera would rotate with it)? Why is the direction in which the object is thrown important?

So what’s the exact goal here? Do you want to ensure the whole object is in view? If that’s the case there’s no easy solution as it’s a complex problem when we talk about arbitrary objects. Though without knowing the expected behaviour and what input variables you have / want to consider it’s difficult to answer this question.

I would suggest axis-aligned bounding box. No need to go through vertices

Ah yes you’re right, thank you !!! I was working with that at first, but then I thought I need to get the position directly above the center of the mesh rather than the uppermost point and when I changed my mind it hasn’t occurred to me I could use it again : - D

Thank you very much for the reply, it helped me to rethink the issue !!! I did not realize that I had to tweak the values that I am getting from the “directionTransform.transform.eulerAngles” with which I replaced the “Vector3.up” so that it can be normalized and be between -1 to 1.

The object might rotate, but it would be rare, since it will not be affected by gravity (therefore the initial direction never changes). My idea was that the camera (blue circle) would follow the object (behind middle of the current uppermost boundary) in the thrown direction but it would not rotate with it. Only possibly lerp back and forth depending on if the object is rotating. (Keeping it taking up the same amount of screenspace at all times is not necessary, but I would like the object not pass through the camera while its rotating)

(green arrow = direction of the throw, black rectangle = object, camera = blue circle, yellow arrow = motion of the camera as the object rotates)

The object is thrown at another object so I would like both of them to be always visible (if the thrown direction is correct). But now I am not sure if running these calculations is not going to introduce too much lag, even when using the bounding box of the object as AscariaQuynn suggested. I will try it out !!!

(I would like the camera remain somehow like this)

Other solutions would be to pre-calculate the spin of the object and get the most distant X and Y coordinates so that the object does not rotate into the camera. Or just calculating the 3D diagonal of the bounding box of the object (which might be further than necessary). And then setting it to coordinates behind the object in the direction of the throw.