I feel like I am missing something. My player object gets spawned at runtime, and so I cant set the target in the inspector. Before using Cinemachine I had a CameraFollow script I just referenced. But with Cinemachine it feels so clunky
I have my Main camera that has the CinemachineBrain on it
I have a Vitural camera
I have a player spawn script that grabs a reference to the Main camera
That I want the script to, after it spawns the player, Pass the player transform to the Main camera, to the CinemachineBrain, which finds the current active camera, and then applies the player transform to the Tracking Target of that camera.
What I find weird is when you try to find the active Virtual Camera that the CinemachineBrain is using, it gives you an interface (ICinemachineCamera), not a reference to the actual VCam (CinemachineCamera)
And what makes it even worse is in older version of Cinemachine that ICinemachineCamera had a Follow and LookAt var, in fact if you head over to the documentation IT STILL SAYS IT HAS THIS! Yet, if you look at the actual script those are gone… like What!!! What kind of software development does that, not a depreciated of Obsolete tag, just gone, and then not update your documentation even. This is why I hate using 3rd party systems, But this was bought by Unity and everyone says it’s amazing so I started using it.
So the solution I found (Here) is so weird and messy, I am thinking there HAS to be a better way to do this, Surely setting follow targets at runtime is a common enough function there is a streamline way to do it.
My janky solution
if (mainCamera != null)
{
CinemachineBrain cinemachineBrain = mainCamera.GetComponent<CinemachineBrain>();
if (cinemachineBrain != null)
{
ICinemachineCamera virtualICamera = cinemachineBrain.ActiveVirtualCamera ;
if (virtualICamera is Component component)
{
component.GetComponent<CinemachineCamera>().Follow = playerObj.transform;
}
else
{
Debug.LogError("Active virtual camera is not a CinemachineVirtualCamera.");
}
}
else
{
Debug.LogError("Main camera does not have a CinemachineBrain component.");
}
}
at first I used this, but this is bad cause I want to be able to access the active camera, not keep log of the index and get it that way
CinemachineVirtualCameraBase cinemachineCamera = CinemachineCore.GetVirtualCamera(0);
if (cinemachineCamera != null) {
cinemachineCamera.Follow = playerObj.transform;
}
else
{
Debug.LogError("No active Cinemachine virtual camera found.");
}