Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

[Fix] Cinemachine 2.0 camera destroyed problem

Discussion in 'Cinemachine' started by caosdoar, Jun 13, 2017.

  1. caosdoar

    caosdoar

    Joined:
    Oct 29, 2016
    Posts:
    26
    Hi,

    I am having a problem when working with Cinemachine virtual cameras and multiple scenes in the editor.

    What I have is a "master scene" that contains several shared game objects, like the main character or the camera. This scene loads a playable area that includes all the virtual cameras for that area. When working on the editor both scenes are loaded in the hierarchy view and the "master scene" set as the active scene. This allows me to work on the playable area, hit run and test it.

    But in the process of unloading the playable area scene and loading it again by the "master scene" the CinemachineCore.mActiveCameras keeps a list of destroyed cameras. Probably the list while in edit mode. And the cameras does not work in game anymore.

    I can work around it by manually removing the playable area scene from the hierarchy before running the game and everything works just fine. But it might be good to have a clean transition between in-editor and in-game of the CinemachineCore state.

    I could not figure out completely why are destroyed cameras still listed but it looks like CinemachineVirtualCameraBase.OnDisable() calls UpdatePriorityQueueStatus and that is going through the path that adds the camera to CinemachineCore.

    Is anyone experience this or is it a known issue? Is there a dev that can help me with it? Should I report it as a bug?

    Thank you!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,319
    I have not seen this problem before. Normally, OnDisable() should remove the vcam from the static list. Besides, there is also an OnDestroy() that does the same thing. Is it possible that OnDisable() and OnDestroy() are somehow being bypassed in your situation? Perhaps you are not unloading the playable area in an entirely kosher manner?
     
  3. caosdoar

    caosdoar

    Joined:
    Oct 29, 2016
    Posts:
    26
    The way that the virtual cameras are unloaded is by not marking them as "DontDestroyOnLoad" and then loading the playable area scene from a script with "SceneManager.LoadScene". I know this is a corner case and probably no one is experiencing this issue. I will try with the new release of Cinemachine and if is still happening I might try to get a minimal repro case and figure out what is going wrong.

    By the way, I think some admin marked this thread as a sticky note by mistake.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,319
    Yes, a repro project would be helpful here. Feel free to send it to me if you like.
     
  5. caosdoar

    caosdoar

    Joined:
    Oct 29, 2016
    Posts:
    26
    Hi Gregory,

    I found out the real source of the problem, it has to be with CinemachineBrain camera blending and the destroy of virtual cameras. If the current active camera gets destroyed CinemachineBrain.mActiveCameraPreviousFrame references a destroyed instance and it breaks as soon it tries to access any data.

    I am attaching a project to reproduce the problem. It has two scenarios: The difficult one, with the master and playable area scenes I explained before; and one more simplier than that. I'll explain how to reproduce it with the later:

    The scene you have to open is Onescene/scene. Let me explain the set up:
    - One camera with a cinemachine brain. It has a custom blends, this is important because otherwise the problem does not happen.
    - Two virtual cameras. Camera 2 with higher priority.

    How to reproduce it:
    - Hit run
    - Delete "CM vcam2"
    - Done!

    The same thing was happening when switching from one playable area to another (or when it was been reloaded). The active cameras list looks it is correct. The only modifications to cinemachine on that project is a few Debug.Log to see when cameras are added or removed from the active list.

    I spent some time trying to find a way to fix this but I will have to go to deep on the cinemachine code. My thinking is that the virtual camera needs to inform that is been destroyed and not only disabled, and that has to be passed to the brain so it can invalidate the mActiveCameraPreviousFrame. This has a problem though, no blend will be applied because of that. Other option is to rethink blending to not access the ICinemachineCamera itself but the data needed for the blending only (the CameraState maybe?). I did not look into this second option too much as it changes quite a bit the system.

    Please, let me know if I can be of any help.
     

    Attached Files:

    Gregoryl likes this.
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,319
    @caosdoar Hey thanks for that! I'll download and have a look.
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,319
    @caosdoar Thanks for the great detective work! The Cinemachine team salutes you. Here is the fix.
     

    Attached Files:

    Adam_Myhill likes this.
  8. caosdoar

    caosdoar

    Joined:
    Oct 29, 2016
    Posts:
    26
    Gregory, that is awesome! I was hoping for a response but I was not expecting a fix.

    Now, watching the changes I feel like a fool. I tried comparing mActiveCameraPrevioiusFrame with null and it did not work (something I must have done wrong) and I guessed it was not possible that way. I have to study the .NET basics again.

    Just as a confirmation. I tested all the scenarios I was having problems with and it works perfectly.

    Salute the whole Cinemachine team for me.

    Cheers.
    Oscar
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,319
  10. caosdoar

    caosdoar

    Joined:
    Oct 29, 2016
    Posts:
    26
    @Gregoryl that is great. Thank you again for all the help!
     
    Adam_Myhill likes this.
  11. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    14
    Hi I have the same problem with the Cinemachine 2.2.0 from the Package Manager Any chance to see this fix there?
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,319
    Yes, CM 2.2.6 is lurking in the shadows (it's in the staging repository) and the problem has been fixed there. It will be publicly available within a couple of weeks.