Search Unity

CinemachineBrain.Start takes a long time when instantiating prefab

Discussion in 'Cinemachine' started by dgoyette, Mar 19, 2018.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    I'm using Cinemachine for a specific game mechanic: When the player dies, I float a camera near the player's ragdoll. For this, I instantiate a prefab that looks like this:

    upload_2018-3-18_22-3-43.png

    It works fine, but on the frame I instantiate it, I'm getting a pretty hard performance hit as CinemachineBrain starts up:

    upload_2018-3-18_22-2-49.png

    I've tried keeping a CinemachineBrain in my scene when the scene loads, rather than on the prefab, but that caused the Free Look not to lock on anymore. I've tried instantiating the prefab when the scene loads, then disable it, and reenable it again when the player dies, but I still get the CinemachineBrain.Start hit at that point.

    I'm curious if there are any other approaches I can take to preloading the CinemachineBrain when the scene loads, so that I don't have hiccups instantiating this Prefab on the fly.

    Thanks,

    -Dan
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    @dgoyette Making sure I understand what you're trying to do. You have a non-CM camera for normal gameplay, and at key moments you want to cut to a CM FreeLook camera. Is that right?

    First of all, you haven't quite nailed the correct way to set up CM. Set it up like this:

    Game object 1:
    - Camera component
    - CinemachineBrain component

    Game Object 2:
    - CinemachineFreeLook component
    - CinemachineCollider extension

    If I were you, I would also have Game Object 3:
    - Your camera script
    - CinemachineVirtualCamera component, with "Do nothing" in Aim and Body sections, so that its transform is 100% driven by your camera script

    The virtual cameras are separate game objects from the Unity Camera. The Brain is aware of all virtual cameras in the scene, monitors them, and drives the Camera's transform based on data that they provide.

    With this setup, the brain is active on the camera right from the start. Activate Game Object 3 for normal gameplay. The camera will behave as though your custom script were attached to it. When the key event happens, enable Game Object 2. The camera will transition to the FreeLook, and then transition back when you disable it. You can set up the transition to be a cut or a blend, as you please.

    I think you'll find this to be a powerful paradigm, and it will open the door for lots of fun and creative camera work. And you won't get the performance spike.
     
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Thanks very much, Gregoryl. I followed you approach, and the difference is significant. I appreciate the detailed steps to resolving this. In most cases now, when I enable Game Object 2, there is almost no Cinemachine impact on the performance on that frame. Thanks again. And the general concept you paid out makes sense.
     
    Gregoryl likes this.