Keeping All Objects In View in Unity
Cinemachine’s Target Group Camera component, allows us to easily set multiple Look At targets, as well as applying rules as to how the camera keeps all targets in view.
When adding this component to our scene, we get a Virtual Camera, with a child object containing a Cinemachine Target Group. Within this Target Group, we can add as many game objects as we like, along with a weighting variable, and a radius to increase the object’s bounding box for any position calculations.
In addition to these two variables, we can also manipulate how to calculate the position and rotation of the group, and when in script execution order this calculation occurs.
For the position, we can set this to be either the center of the objects and any additional radius, or we can use the average, based upon the object’s weights and their individual positions.
For rotation, we can set it to manual, which uses the target groups transform. A more complex solution can be to use an average of the individual object’s orientations.
With our Target Group set up, we can now use this transform for our Look At target. From here, we simply follow all our standard virtual camera Aim rule sets, to define how the camera is manipulated to keep the Target Group in shot.
We use the Group Composer Aim type, which provides us the additional settings of framing size, mode, and damping. We can then determine how the camera makes it’s adjustments — whether it be zoom only, dolly only, or dolly then zoom.
Depending on which adjustment mode is set, we can set a minimum and maximum field of view for zooming. For dollying, we can set the min and max dolly in and out, which allows us to set distance limits at which the target objects stop manipulating the camera, as well as min and max distance to determine the proximities at which the camera can be from the target objects.
Whether we move all target objects, or individual ones, our camera will work to keep them all within shot!