Finding farthest point on top of game object (geometry)

In the below scene, I am trying to calculate the farthest point from the camera at the top of each cylinder (designated roughly by the green dots):

alt text

Assuming the object’s height and radius are known, and the transform position of both the object and the camera, what is the most efficient way to do this?

This is what I came up with:

float xDiff = transform.position.x - Camera.main.transform.position.x;
float zDiff = transform.position.z - Camera.main.transform.position.z;
float angle = Mathf.Atan2( xDiff, zDiff );

float zAdjust = Mathf.Cos( angle ) * radius;
float xAdjust = Mathf.Sin( angle ) * radius;

barPos = Camera.main.WorldToScreenPoint( new Vector3( transform.position.x + xAdjust, transform.position.y + height, transform.position.z + zAdjust ) );

But it seems to have a somewhat heavy hit on my frame rate. I still have some other kinks in my code I need to work out, so I’m not even sure if my geometry is 100% correct, but if it’s going to be too expensive to update every frame I figured I should seek a better alternative. Any ideas?

It seems like you just want the longest distance to all you points or transforms.

if so then get a list of your transforms, and for each object do

//get the length of the vector
(obj.position - camera.posion).length

maybe throw and Math.Abs on it depending on the direction

you can then save and compare to next length

float maxDist = Mathf.Max(maxDist, (obj.position - camera.posion).length);

then at the end of the loop you have maxDist.