EDIT: Here are some simple scripts that will move the camera a set distance away from an object and look at it. Imagine an object rests at (0,0,0) and the camera is looking at from (3,3,3). These scripts will move the camera to be the same distance away on the x and z-axis while staying at height 3. This rough, doesn’t care about the height of the GO, and sets itself to a specific distance away. You can adjust this script for varying sized objects simply by testing out what you think looks good and storing that value in the offset variable on the object.
On your GameObject:
float offset = 4.24264f
On your main camera:
public void Focus(Transform target, float offset)
Vector3 direction = transform.position - target.position;
direction = offset * direction.normalized;
Vector3 moveTo = target.position + direction;
transform.position = new Vector3(moveTo.x, 3f, moveTo.z);
I hope this works for you. If you need something a little more dynamic, you could use the height as a factor multiplied against a static offset. Like if the GO is 2 units tall you could move it 2 * offset away. And just look up the collider height to feed that value.
Well, first take a look at Transform.LookAt.
Then it gets hairy unless there is an API call for this that I can’t find (like your example from the unity editor). If you have a collider on the thing you’re trying to frame, you could look at it’s dimensions, then adjust the current viewport to the larger one respective to your current aspect ratio (plus your padding).
Maybe Camera.WorldToViewportPoint and Camera.ViewportToScreenPoint will come in handy.
Whatever math you use to figure out the new rectangle you’re looking at based on the collider you wanted to frame, do that again except figure it out based on the width of the group of objects you have selected. Find the leftmost and rightmost objects in the group after you’ve turned the camera to look at them, and use that as your bounds (plus your pladding). You can probably use Bounds.Encapsulate to accomplish this actually, then it’s a repeat of the simple version.
I realize there’s a lot of code you’re going to need for this, so hopefully someone else knows a more API based “FrameInView” method or something.