Cinemachine Timeline Clip Search for Camera

Hi there!

I’m using cinemachine tracks/clips to have certain objects (that have the timeline) control how cameras blend.

My question more of a workflow question: What is the best way to populate the virtual camera reference of a specific cinemachine clip on a timeline at run time?

My context looks like this: I have a prefab that has a timeline, a cinemachine track on the timeline, and three cinmachine clips that blend from one to the other in sequence 1-> 2 → 3.

Clip 2 references a virtual camera that sits as a child of the prefab, so no problem there.
Clips 1 and 3 however need to find the “Main” virtual camera and hook up those references when the prefab gets instantiated.

What is the best way to do this?

Thanks so much!

1 Like

To set the virtual camera on a timeline at runtime, you need to bind the clip to the virtual camera through the playable director playing the timeline.

Here is an example:

    void SetVirtualCameraToClip(CinemachineTrack track, PlayableDirector director, string clipName, CinemachineVirtualCamera camera)
    {
        foreach (var clip in track.GetClips())
        {
            if (clip.displayName == clipName)
            {
                var asset = clip.asset as CinemachineShot;
                if (asset != null)
                    director.SetReferenceValue(asset.VirtualCamera.exposedName, camera);
            }
        }
    }
2 Likes

Hi @seant_unity !

Thanks so much for the quick reply :slight_smile:

I figured this might be the only way to go about it… I do have a quick follow up question though as I’m still trying to wrap my head around exposed references. I recall running across this thread a little while ago because I ran into a similar issue the last time I tried to set clip variables at edit / run time (funny enough its you in that thread!):
https://discussions.unity.com/t/699388

Basically, before I set the reference value of the clip’s VirtualCamera exposed reference, do I need to assign the exposedName a unique GUID so that all the clips on that track don’t have their VirtualCamera reference changed?

Something like this?

asset.VirtualCamera.exposedName = UnityEditor.GUID.Generate ().ToString ();
director.SetReferenceValue (asset.VirtualCamera.exposedName , camera);

If not, why not? When should I / should I not be be assigning unique names to exposed references before setting their values?

Thanks again for your help! I really appreciate it :slight_smile: