Keep GameObject in Camera View as it Grows Vertically

I have a group of gameobjects that “grow” vertically (think of a plant growing). The camera view starts at the ground level, where the plant sprouts from the dirt. I would like to have the camera smoothly zoom out as the plant grow to keep the entire plant in view. How would I go about doing this?

I know that I can use the minimum/maximum Y values, but how do I use that to position the camera so that the objects are within the camera’s field of view?


probably not the most efficient way since my maths skills are still a bit rusty, but it will work (or at least give you a starting point) :slight_smile:

public GameObject m_target;

void Update () 
	float gamma = camera.fieldOfView/2f;
	float alpha = 90f-gamma;
	float c = Mathf.Abs(m_target.transform.localScale.y/2f);
	float sinA = Mathf.Sin(alpha*Mathf.Deg2Rad);
	float sinC = Mathf.Sin(gamma*Mathf.Deg2Rad);
	// a/sin(alpha) = c/sin(gamma)
	// a = (c*sin(alpha))/sin(gamma)
	float a = (c*sinA)/sinC;
	Vector3 newPos = m_target.transform.position; // assuming the object pivot is at its center
	newPos.z -= a+3; // give it a bit of extra space
	transform.position = newPos;