Search Unity

replace Cinemachine Shot in timeline during runtime

Discussion in 'Cinemachine' started by Doronn, Jun 30, 2019.

  1. Doronn

    Doronn

    Joined:
    Sep 17, 2018
    Posts:
    5
    Posting here too in case the other forum wasn't right:

    Hi.
    I'm trying to make a timeline sequence which can change in some situations (the change is decided before the sequence starts playing)

    Specifically replace a virtual camera in one of the shots clip.

    But I am not able to figure out how to replace my CinemachineShot clip in the Cinemachine track.

    I tried to simply apply a different virtual camera by code, it worked but the clip got broken and the virtual camera properties in that shot were not applied. when the timeline plays it ignores the clip altogether as if in that point there is no virtual camera active at all.

    Any help about this would be appreciated.

    Edit:
    this is the code for I tested with:
    Code (CSharp):
    1. public static void ReplaceClipCinemachineCamera(this PlayableDirector playableDirector,
    2.             string trackName,
    3.             string clipName,
    4.             GameObject otherVirtualCamera)
    5.         {
    6.             TimelineAsset timeline = playableDirector.playableAsset as TimelineAsset;
    7.  
    8.             if (timeline == null)
    9.             {
    10.                 return;
    11.             }
    12.  
    13.             foreach (var trackAsset in timeline.GetOutputTracks())
    14.             {
    15.                 if (trackAsset.name == trackName)
    16.                 {
    17.                     var clipToEdit = trackAsset.GetClips()
    18.                         .FirstOrDefault(clip => clip.displayName == clipName);
    19.                     if (clipToEdit == null)
    20.                     {
    21.                         continue;
    22.                     }
    23.  
    24.                     var controlPlayableAsset = (clipToEdit.asset as CinemachineShot);
    25.  
    26.                     if (controlPlayableAsset == null)
    27.                     {
    28.                         return;
    29.                     }
    30.  
    31.                     playableDirector.SetReferenceValue(controlPlayableAsset.VirtualCamera.exposedName, otherVirtualCamera);
    32.                 }
    33.             }
    34.         }
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,947
    If you can't get the replacement code to work, there is perhaps another alternative.
    Could you use a CinemachineMixingCamera as a target vcam for the clip? Give it different vcam children, and instead of trying to remap the clip, leave the mapping alone and instead set the weight in the mixing camera of the child you want to activate. All other child weights to 0.
     
    Last edited: Jun 30, 2019
  3. Doronn

    Doronn

    Joined:
    Sep 17, 2018
    Posts:
    5
    Thank you for the quick answer.
    I will try to make the required changes to support the mixer camera.