Search Unity

Cinemachine vs Final IK - script execution order nightmare

Discussion in 'Cinemachine' started by mariusgeorge, May 17, 2019.

  1. mariusgeorge

    mariusgeorge

    Joined:
    Jan 29, 2016
    Posts:
    18
    Hi everyone,

    When a Cinemachine vcam is targeting something in a character's skeletal hierarchy, and that hierarchy is affected by IK, the vcam will target the wrong location, causing a terrible 1-frame latency and jitter situation.

    Final IK forces a certain order via its scripts, as shown below. So I attempted to drop CinemachineBrain below that. However - CinemachineBrain ALWAYS resets itself to 100, no matter what.

    This effectively means that it is impossible to get things into the correct order.

    Did anyone come across a solution or work-around for this?

    Please see here:

    script-order.png

    I will also reference this thread, which seems to imply it IS possible to move CinemachineBrain execution order: https://forum.unity.com/threads/tak...machinebrain-updates-camera-positions.547049/

    Any ideas?

    Thanks!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    The problem is that CM is now a read-only package, and the script execution order is baked into the CMBrain's meta-file. Because the file is read-only, you can't modify it without doing a little extra work.

    Here are 2 possible workarounds:
    1. Make CinemachineBrain.cs.meta writable in your package cache (every Unity seat using the project will need to do this), or
    2. Embed the Cinemachine package into your project (copy it to your packages directory), then you can mod it and put it in source control.
     
    chemicalcrux and mariusgeorge like this.
  3. mariusgeorge

    mariusgeorge

    Joined:
    Jan 29, 2016
    Posts:
    18
    Thanks for the suggested work-arounds. I wonder if there can be a more permanent solution in the near future, like being able to modify the execution order via some asset or property exposed on the CinemachineBrain?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Actually this is a more general problem that Unity needs to solve re. packages
     
    firelight_jaden and mariusgeorge like this.
  5. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    166
    Almost 2020 and this problem makes the use of cinemachine for a serious project impossible.

    The square orange gizmo is the "Follow" transform, while the orange circle is the "LookAt" transform.
    So at this point, is there a permanent solution to that? and please don't tell me to use a "work around", i'd rather not use cinemachine at all.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    That the script execution order is baked into the package is not a Cinemachine-specific problem, but a more general Unity scripting and packages limitation.

    Currently, it is possible to customize the script execution order of package scripts. You do it by embedding the package source into your project, and modifying the script execution order of the embedded script. It's like any other customization of package functionality.
     
  7. transat

    transat

    Joined:
    May 5, 2018
    Posts:
    779
    What's the recommended way to do this on a mac?
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    I think you're better off embedding the whole package in your project and modifying it there. It's less error-prone.
     
  9. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I think it would be a good idea to have Cinemachine update via API calls, a manual update if you will, just like Physics, Animator, Navmesh Agent or anything else. With Unity's Physics, you can update whenever you want.

    This means API can be as simple as Cinemachine.CinemachineBrain.autoUpdate = false;
    Followed by a brain update call in the developer's own code, in the order the developer wants.

    Then anyone with conflicts like this easily manage it, just like other parts of Unity - animators, physics, agents etc. It's a proven and known working thing already in Unity.
     
    Gregoryl likes this.
  10. transat

    transat

    Joined:
    May 5, 2018
    Posts:
    779
    Cool. Would that impact on my ability to download future CM updates through the package manager if I move the package to the Asset folder? And if not, would my change get overwritten with every update?

    In other words, should I just the drag the folder over or should i do a git clone in my Assets folder and do the updating myself?
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Sadly, you will have to actively manage this.

    A git clone into your assets will import a lot of stuff that you don't actually need, but on the other hand might make updating a little easier.

    If you just copy the package over, then you will have to set the script order again every time you upgrade, unless you take care not to overwrite your local CinemachineBrain.cs.meta file which is where the order info is stored. With current versions of package manager, the copy needs to be done manually (future versions will more explicitly support package embedding - which is what you're doing).
     
    transat likes this.
  12. a_engel123

    a_engel123

    Joined:
    Dec 25, 2020
    Posts:
    4
    How would you make CinemachineBrain.cs.meta writable? I tried just editing the file outside of unity but that doesn't seem to do anything, although doing this with a normal script that I wrote does change the script execution order.
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Use solution #2: Embed the Cinemachine package into your project (copy it to your packages directory), then you can mod it and put it in source control.
     
  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Revisiting this thread from the distant future. ManualUpdate was added to CMBrain, thanks @hippocoder for the suggestion.

    If you add this custom script to your main camera with the CMBrain, it will drive the update and you can set its execution order to be what you need.

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. public class ManualUpdateCM : MonoBehaviour
    5. {
    6.     CinemachineBrain brain;
    7.  
    8.     void Start()
    9.     {
    10.         brain = GetComponent<CinemachineBrain>();
    11.         brain.m_UpdateMethod = CinemachineBrain.UpdateMethod.ManualUpdate;
    12.     }
    13.  
    14.     void LateUpdate()
    15.     {
    16.         brain.ManualUpdate();
    17.     }
    18. }
    19.  
     
  15. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    519
    Works like a charme, was setting FixeUpdate to allow the follow correctly but with microstutters, now it's butter smooth along with FinalIK
     
    Last edited: Nov 29, 2022
    Gregoryl likes this.
  16. KinanGH

    KinanGH

    Joined:
    Dec 3, 2019
    Posts:
    37
    I tried this but the jitter still exists (I set the execution order of this script larger than the brain btw). Setting the brain to update in 'FixedUpdate' is far more smooth then 'Manual' but still, there is a little stutter in the parallax background layers.
     
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    There are many possible causes for jitter. If you would like to start a new thread describing your setup and the issue you are having, we can look at it.