Search Unity

Does a cinemachine cut always happen instantly?

Discussion in 'Cinemachine' started by makeshiftwings, Mar 26, 2021.

  1. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    I've got a situation where I want to temporarily snap the main camera instantly to a virtual cam that has a framing transposer, do some screen calculations based on that main camera, then snap it instantly back to where it is supposed to be. I have basically this code in a coroutine:

    Code (csharp):
    1. _cinematicBrain.m_DefaultBlend = new CinemachineBlendDefinition(CinemachineBlendDefinition.Style.Cut, 0f);
    2. _theNewVirtualCamera.gameObject.SetActive(true);
    3. _theOldVirtualCamera.gameObject.SetActive(false);
    4. yield return null;
    5. DoTheImportantCalculation();
    That should give it exactly one frame / one call to LateUpdate to do its thing. It works correctly 99% of the time, but every once in a while the ImportantCalculation is wrong, as if the actual MainCamera didn't get to its position instantly or the Framing Transposer didn't kick in fast enough. Do these things always happen instantly on a cut, or is there sometimes a delay by a few frames? That's the only thing I can think of that would explain the behavior I'm seeing.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    That seems kind of nutty to me. Why not instead just set the main camera's transform to be the same as the vcam's, do the calculation, and then set it back?
     
  3. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Because I don't know where the vcam's transform will actually be until the camera cuts to it and it does its Framing Transposer magic. I also need it to be zoomed correctly; the ImportantCalculation is basically trying to get the screen coordinates of the world objects that the vcam will be pointing at; the problem is I need to get those coordinates right when the scene is loaded, before the camera actually gets over to the vcam.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    The switchover should happen right away during CinemachineBrain.LateUpdate().

    However, you needn't mess with the blend and activate/deactivate vcams. There are other ways. Here are a couple of alternatives:
    • CinemachineBrain.SoloCamera - this static property temporarily overrides the priority system and forces a specific vcam to become live in all Brains (probably this is the cleanest option for you);
    • CinemachineBrain.SetCameraOverride() and CinemachineBrain.ReleaseCameraOverride() - another way to override the priority system (timeline uses this).
    Another useful tidbit: CinemachineCore.CameraUpdatedEvent is fired as soon as the Brain updates the camera position. That would be a good moment to do your important calculations.

    As to why the calculation sometime comes out wrong, that's hard to tell without knowing more about the vcam in question and the context. If it's tracking something and has damping, it might be in a cold state when activated, and maybe it doesn't warm up fast enough. To keep it warm, you can set its standby update setting to Always.
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    And anyway, don't you get a flicker when you move the main camera for a frame then move it back?

    You could create another Camera object and keep it inactive and just use it for making your important calculations. You can pull the info out of the vcam and apply it to the second Camera, then do the calculations, all in the same frame without a coroutine - so long as the vcam stays warm.

    Another thought: if the vcam isn't warm, you can force it to update to its final position by calling vcam.UpdateCameraState(Vector3.up, -1);
     
    Last edited: Mar 27, 2021
  6. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Awesome, thank you! This is all very valuable information. And yes there is a flicker when the scene loads but I was willing to live with it; CinemachineBrain.SoloCamera might solve my problems, and I didn't think about having a second camera just to do screen space calculations; that actually might make my life much easier.
     
    Gregoryl likes this.