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):
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?