I have been working on my this aspect of my project since last night and have finally got this code to work exactly as I want it to.
However if the first target is removed before the second one, I get an out of range exception. I would like to know if there is a better way to fix my problem.
void LateUpdate()
{
if (targets.Count == 0)
return;
Move();
Zoom();
}
void Move()
{
GetBoundsData();
Vector3 newPosition = new Vector3(centerPoint.x + offset.x, transform.position.y, centerPoint.z + offset.z);
transform.position = Vector3.SmoothDamp(transform.position, newPosition, ref velocity, smoothTime);
}
[SerializeField] Bounds bounds;
[SerializeField] Vector3 centerPoint;
public Vector3 maxBoundsPoints;
void GetBoundsData()
{
if (targets[0] == null)
{
targets.RemoveAt(0);
}
if (targets.Count == 1)
centerPoint = targets[0].position;
SetUpBounds();
centerPoint = bounds.center;
maxBoundsPoints = bounds.max;
}
void SetUpBounds()
{
bounds = new Bounds(targets[0].position, Vector3.zero);
for (int i = 0; i < targets.Count; i++)
{
if (targets[i] == null)
{
targets.RemoveAt(i);
}
bounds.Encapsulate(targets[i].position);
}
}
Bounds bounds = new Bounds(lastPos, Vector3.zero);this line produces an error though i think may have a good solution now thanks to your well-done explanation look in the comment on the answer by DMGregory \$\endgroup\$