Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question The camera will follow and jitter, in a custom time step.

Discussion in 'Cinemachine' started by Lethe23, Aug 20, 2020.

  1. Lethe23

    Lethe23

    Joined:
    Jun 17, 2019
    Posts:
    10
    Code (CSharp):
    1.  
    2.     public class Test : MonoBehaviour
    3.     {
    4.         public CinemachineBrain brain;
    5.         public CinemachineVirtualCamera virtualCamera;
    6.         private void Awake()
    7.         {
    8.             Physics.autoSimulation = false;
    9.             brain.m_UpdateMethod = CinemachineBrain.UpdateMethod.ManualUpdate;
    10.             brain.m_BlendUpdateMethod = CinemachineBrain.BrainUpdateMethod.LateUpdate;
    11.  
    12.             virtualCamera.Follow = transform;
    13.             virtualCamera.LookAt = transform;
    14.         }
    15.  
    16.         private float t;
    17.  
    18.         private void Update()
    19.         {
    20.             float dt = 1 / 50f;
    21.             t += Time.deltaTime;
    22.             while (t >= dt)
    23.             {
    24.                 t -= dt;
    25.                 Tick(dt);
    26.             }
    27.         }
    28.  
    29.         private void Tick(float deltaTime)
    30.         {
    31.             var v = Input.GetAxis("Vertical");
    32.             var h = Input.GetAxis("Horizontal");
    33.             var force = new Vector3(h, 0, v) * deltaTime * 25;
    34.             GetComponent<Rigidbody>().AddForce(force, ForceMode.Impulse);
    35.  
    36.             Physics.Simulate(deltaTime);
    37.  
    38.             CinemachineCore.UniformDeltaTimeOverride = deltaTime;
    39.             brain.ManualUpdate();
    40.         }
    41.     }
    Is there any problem with me following this way? Why does the camera shake when following it?
    @Gregoryl can you answer above question?
     
    Last edited: Aug 20, 2020
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    That's an unusual way to be doing things. I don't know offhand why it's shaking. Can you send me a lightweight project that shows the jitter?
     
  3. Lethe23

    Lethe23

    Joined:
    Jun 17, 2019
    Posts:
    10
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    @Lethe23 Thanks for preparing that project. ManualUpdate currently does not support that usage - it's designed for a single call to ManualUpdate() per frame. In your case that assumption is violated.

    However, it's not difficult to patch CinemachineBrain.cs so that this use case is supported. I have attached a patched version here; you'll have to embed Cinemachine into your project and overwrite CinemachineBrain.cs. We will include this patch in an upcoming CM release, and when that happens you can once again revert CM to the public version.

    Also, because Cinemachine filters its updates to avoid multiple updates on a single vcam per frame, it's necessary in this case to manually drive Cinemachine's concept of current time, using CinemachineCore.CurrentTimeOverride. I've modified your Test.cs to do that, and attached it here as well.

    With these two mods, your project runs smoothly.
     

    Attached Files: