Accessing Tracked Object Offset for Growth Mechanic

Creature looks for food. Creature grows when food is successfully collected. Problem is Cinemachine stays in place, so if the creature grows too big, the camera doesn’t back up to accommodate size.

So, I figured accessing the tracked object offset or FOV for Cinemachine to adjust the distance from the creature when it grows is what’s best. I’m just struggling to wrap my head around the scripting.

I haven’t worked with Cinemachine a lot, so this is 100% just me being dumb. It’s been a fun few hours in struggle city but I’d like to leave now lol. I’ve always struggled with the documentation. Is someone able to break it down for me please.

using Cinemachine;

public class playerscript : MonoBehaviour
{
 
    public GameObject creature;
    public CinemachineVirtualCamera virtualCam;
 
    // Start is called before the first frame update
    void Start()
    {
        camOffset = virtualCam.GetCinemachineComponent<CinemachineComposer>().m_TrackedObjectOffset;
    }

  private void OnTriggerEnter(Collider collision)
{     
        if(collision.gameObject.tag == "Food")
        {
            Destroy(collision.gameObject);
            Growth();
        }
    }

    private void Growth()
    {
        creature.transform.localScale += new Vector3 (0.03f, 0f, 0.03f);
        // push back/pull forward camera to accommodate new size
    }
 
}

Can you show the vcam inspector so I know what components are attached?

There are several ways to get this to happen:

  • Widen the FOV
  • Increase the camera distance
  • Some combination of the above

You can set these values directly in the CM components, or you can automate it by adding your creature to a TargetGroup and putting a GroupComposer in the vcam. In the target group, you can specify the radius of the target, and you can set up the vcam to make it adjust to the growing creature.