Adding VirtualCameras to ClearShot Camera at RunTime

If I generate a list of Vector3 positions is there a way to programatically add virtual cameras to a clear shot camera at each of the positions. I would be looking to add a standard follow camera and have its follow be an invisible game object at the position and its follow be another game object, or do I have to manually add 400 cameras and then set each of their variables in the code.

I looked into the ChildCameras[ ] but it is get only.

Thanks

You can add vcams to your clearshot at run-time. Create vcams at your positions, and child them to the clearshot camera. See: Does ClearShot require child virtual cameras to actually be children of the ClearShot?

I can see that but the ChildCameras[ ] never got updated to include the added cameras… What I came up with was

  1. Create an Empty Prefab
  2. Add a CinemachineVirtualCamera component to it
  3. Set the game object parent to be the Clear Shot Camera
  4. Disable the Clearshot Camera and Reenable it (This caused the cameras to be added to the ChildCameras[ ]
1 Like

For anyone coming in here looking at this here is the code I used

CinemachineVirtualCamera vcam = new GameObject().AddComponent<CinemachineVirtualCamera>();
vcam.name = "YourName";
vcam.transform.SetParent(theclearshotCameratransform);
vcam.transform.position = Vector3.zero; // Set to your desired position
vcam.m_Lens.FieldOfView = 42; // set to desired fov 

CinemachineCollider tvCollider = new CinemachineCollider();
tvCollider.m_CollideAgainst = LayerMask.GetMask("Default");
vcam.As<CinemachineVirtualCameraBase>().AddExtension(tvCollider);
1 Like

Instead of enable/disable, you should call OnTransformChildrenChanged on your Clearshot vcam, like this:

clearshot.OnTransformChildrenChanged(); // CinemachineClearShot clearshot;

This will update ChildCameras internally.

1 Like

Thanks