Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Cinemachine FixedUpdate not syncing with fixedupdate movement? (2.2.0)

Discussion in 'Cinemachine' started by KubeX, Jul 22, 2018.

  1. KubeX

    KubeX

    Joined:
    Feb 15, 2013
    Posts:
    122
    So I've ran into some minor issues here which I couldn't figure a workaround without the use of a script.

    Issue: Cinemachine FixedUpdate has some jittery movement when following the player. A custom made script without the use of Cinemachine properly lines up with the player through FixedUpdate.

    This video shows the jittery movement between the player and the cinemachine camera through fixedupdate. They aren't lining up properly. (You might need to watch the video in full screen to see the slight jittery movement)


    Smart update is interesting since it gets rid of the jittery movement most of the time, except when the player performs a dodge roll, and moves right after. This is because the smart update switches to late update for a moment then reverts back to fixed update. While the player is moving, there are a few times where the smart update will switch to late update, long enough for it to show jitter before switching back to fixed update. This video shows the smart update jitter after a dodge roll.


    This one shows the custom made script using fixed update. The camera and player are lined up perfectly. The player also doesn't have jittery movement after a dodge roll.



    So you may be asking, "Why not just use your custom script instead?". While I could do that, I'm missing out on other Cinemachine features. I'd have to recreate similar features Cinemachine has, when Cinemachine already provides them. The reason I'm posting this is because I want to know if there is a way to get Cinemachine's fixed update to update in sync with the player's movement. The fact that Cinemachine's fixed update isn't working like how my script's fixed update is syncing up the camera's movement and the player's movement, is confusing me.

    Other notes:
    -The player is a rigidbody2d, with Interpolation disabled. Enabling Interpolation didn't help with this.
    -Fixed timestep is 0.00833333 (I tried other numbers, but didn't seem to help.)
     
    krou likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    FixedUpdate should work all the time. It updates the vcam on FixedUpdate, just like your custom script.

    That said, there were some issues with SmartUpdate that got fixed recently. Could you see if you still get the jitter with Cinemachine 2.2.6? You can get a preview version of it from the Unity staging repository.
     
  3. KubeX

    KubeX

    Joined:
    Feb 15, 2013
    Posts:
    122
    I could try. How do I access the Unity staging repository?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    With a text editor edit your project's Packages/manifest.json file and add this line:
    "registry": "https://staging-packages.unity.com",
    immediately before the "dependencies" line. Then open Package Manager, and you will see the experimental and unpublished content in addition to full-fledged releases. Use at your own risk.
     
  5. KubeX

    KubeX

    Joined:
    Feb 15, 2013
    Posts:
    122
    Thanks. I gave it a try and the smart update no longer seems to use fixed update at all in that version. Camera smoothing was also not working, even though the virtual camera settings were set to have smoothing. I reverted it back to the original version. I'll just have to find a work around this issue.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    SmartUpdate will choose LateUpdate if the target is animated using both LateUpdate and FixedUpdate. Is that what's happening? By animated I mean that the target's transform gets changed.

    To force FixedUpdate only, set the brain to FixedUpdate.
     
  7. KubeX

    KubeX

    Joined:
    Feb 15, 2013
    Posts:
    122
    Yeah, I tried FixedUpdate and it was very similar to the previous one.
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    I'd like to drill into that a little more. I don't understand why the Brain in FixedUpdate would behave differently from your script in FixedUpdate. What's different?

    I know the brain has a very late execution order, it would update after everything else. Could that have something to do with it?

    Is there anything special happening when you see the jitter? Is the target being animated differently at that time?
     
  9. KubeX

    KubeX

    Joined:
    Feb 15, 2013
    Posts:
    122
    Perhaps. The jitter is most likely because the player and the camera movement aren't exactly lining up. Being a pixel behind can seem like a big difference. On a non-pixelated game, this might be less noticeable. I've had this similar issue before with my own scripts (that is, before I used Cinemachine). After I made the camera related scripts use FixedUpdate, it works.
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    In FixedUpdate mode, the brain is using FixedUpdate.
    So why doesn't it work the same as your scripts? It shouldn't be off at all, unless your animation is not purely on FixedUpdate (but then your script would suffer also).
     
  11. KubeX

    KubeX

    Joined:
    Feb 15, 2013
    Posts:
    122
    Rigidbody2D is affected by the fixed time step right? I have the player's rigidbody velocity being directly affected by the player input inside Update. But the player's rigidbody2d has no interpolation enabled.

    This is the camera follow script using FixedUpdate, in case you're curious.
    https://pastebin.com/MXUhVbDP

    This one is from the player movement script
    https://pastebin.com/tHiVEUQd
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    Looks ok to me. Maybe it's a pixel-perfect thing. TBH the jitter is very subtle, and I'm not sure I can see it.
     
  13. KubeX

    KubeX

    Joined:
    Feb 15, 2013
    Posts:
    122
    I've discovered something interesting actually! Which could explain the very subtle jitter yet different results.

    I've tested both methods in slow motion (0.1 time scale) and noticed the Cinemachine FixedUpdate appears to be retrieving the new position of the VirtualCamera every fixed timestep but is updating it's own position every frame. Meanwhile, my script is both retrieving the position of the player and updating it's own position every fixed timestep.

    I decided to take this to extreme measures and changed the time step to 0.05 to see the greater difference. I also created a temporary script which resets the player's position upon a key press. This teleports the player to see how the camera is moving.

    That's when the issue seemed to look more obvious. For some reason Cinemachine is switching between LateUpdate and FixedUpdate even though it's clearly set as FixedUpdate.

    Here's the video that shows what's happening.


    Also, I'm using Cinemachine 2.2.0 in this one.
     
  14. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    HI,

    So I have faced something very similar issue. In two cases with cinemachine from asset store and latest 2.2 from package manager.

    I have recorded video, with very minimalistic setup of active objects.



    So basically I got assigned brain to the camera and cinamchine look in this case.
    There is game object, which has simple script, to assign velocity to its rigidbody.
    This object has also children, which is follower and lookAt. These two are assigned accordingly to FreeLook Script.
    From 2min in vid, you can see apparent jittering, when box is traveling. This is with Late Update set.
    While later, setting to fixed update, seams smoother.
    On the video you can find also times, where I change Cienamachine -> Free Look -> Rig -> Body -> Damping, between 0 and some value. Then watching camera behavior.

    However, I had been prototyping following case.
    Same setup as above. But, the cinemachine LookAt and Follower targets are assigned to separate New LookAt and Follower GameObjects. These two New game objects have same parent, of which this parent has a simple script. In Update. Script consists of position and rotation assignment (Transform SetPositionAndRotation), for these new transformation parent (Hence for new LookAt and Follower), which are referenced from the transformation of the moving object. As the result, new LookAt and Follower, are exactly at the same position, as the moving object, with its LookAt and Follower.
    Now, when you watch the camera in FixedUpdate state, it will highly jitter.

    Hope that help with an investigation of the issues.

    PS. As I love the concept of cinemachine, if the jittering issues wont be resolved any near times, I would need to drop it, since I may need an alternative camera system for fast moving objects. All best.

    PS2. Just on side note, you have mentioned regarding late update and possible script execution order. Previously I have tried earlier version of CM (Asset Store), where I could set script execution order. By changing it, I haven't noticed any difference. So not sure, if this has an effect on jittering.
     
    Last edited: Jul 25, 2018
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    Thanks guys for the input.
    @Antypodish can you upload the simple project you talked about? I'd like to trace into it to see what's going on.
    Thanks
     
  16. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    Sure,


    Attached Test
    I just stripped scene to bare minimum, and placed few examples.

    Attaching unityPackage and screenshot.
    You should be able to see something similar.

    Tested on Unity 2108.2.0b4 (64bit)
    With Cineamchine 2.2.0 from package manager.


    Objective
    One of my goals with cinemachine, was to be able follow camera behind moving object at start, as 3rd person camera, and to be able rotate camera 360, while object is moving.
    This lead to series of tests, as of experienced issues.


    Test Results
    Concluding my brief analysis, with given settings, (almost defaults). Adjusting rigs damping.
    All test are based on moving object and cinemachine response.

    #1. Late Update with 0 damping, causes jitter, when camera reaches x axis edges.

    #2. Late Update with damping, causes jitter, when follower object is moving.

    #3. Fixed Update with 0 damping Appears fine. However, unable to rotate camera around the follower 360 x axis, when look at object is at same position as follower object.

    #4. Fixed Update Appears fine. Yet, when aligning with a horizon and wait, you can notice periodical jitter, ever few seconds.

    Additionally to each of above case, changing Orbits -> Binding Mode, don't seams playing nice. Default setting is Follow World Up. Changing to any of mode, seams causes some lag. But I may haven't set something correctly.

    I did build and run the scene, for case #1, just to see, if editor is cause of the issue. but jitter was also apparent aswell.

    Above test are far from being exhausted, but shows the most relevant and experienced to me issues.


    Hope this will allow to find the issues.

    All best.

    PS. If you run in any trouble with scene, let me know.
     

    Attached Files:

    Last edited: Jul 26, 2018
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    Thank you, I will have a look
     
  18. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    Never the less, I decided to write some alternative in meantime. And experienced lot of challenges, as machine is, in terms of fast moving object. One solution I found, which works for me, is to ensure, that calculations take into consideration future position, of follower. Jittering were resolved.

    Not sure how relevant this may be to cinemachine, as I haven't looked in the code, but in following topic
    Matching object Transformation to fast moving RigidBody.
    I have discussed previously very similar matter, and solution which worked for me.

     
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    @Antypodish Thanks for setting up that project. It illustrates what I have been saying all along: you have to animate the cameras and their targets on the same clock, or you will get jitter.

    In your scene, the target is being animated on the physics clock (FixedUpdate). As expected, cameras looking at the target and animated on LateUpdate show jitter, while cameras looking at the target and animated on FixedUpdate are smooth. This exactly illustrates the aliasing effects I've been talking about.

    Putting the camera on Smart Update also gives smooth results, as Smart Update monitors the target and updates the vcam on the same clock that the target is being animated on.

    Why can't you rotate 360 degrees? Because the binding mode is SimpleFollow and the target is moving too fast. The SimpleFollow pulls on the camera as if it were on a string, it is not designed to maintain a fixed relationship except for distance.

    To maintain a fixed relationship you need to set the binding mode to LockToTargetWithWorldUp. Then it's fine, you can rotate all around. You may have to increase the max speed in the X axis to get a more responsive action, but generally it seems fine to me.

    I don't know what you mean when you say you have lag in the non-SimpleFollow modes. I'm not seeing anything like that.
     
  20. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    Ok, for most things my bad.
    Many thanks for taking your time.

    Indeed changing to fixed update and Lock to Target, resolves all I need.

    I was aware, of FixedUpdate and LateUpdate differences, but reason I was looking into them, was that only with LatUpdate, I was able to rotate camera, as I wanted, with 0 damping. Ok that's solved.

    One thing only left is FixedUpdate with Damping and periodical jitters. Using same rig I sent you, I performed simple short test, how to replicate issue. Or maybe is on my Unity only?

    Video if not there, should be in few min (uploading).


    Initial jitters you should see, just after T 0:50.

    Basically:
    Enabling Fixed Update With 0 Damping object.
    Starting simulation,
    Assigning rig damping along Y
    Orient camera to relevant rig.
    Wait few seconds and see periodical jitter.

    I hope you can replicate that.
     
    Last edited: Jul 27, 2018
  21. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    CM with damping doesn't respond well to frame time spikes. When you get one, there will be a pop in the camera because damped position is calculated based on the position on the previous frame.

    It's normal that you'll get occasional frame spikes when running in the Editor, because the editor has all sorts of overhead not present in the game build. You should not see these in a build.
     
  22. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    Seams that is to be correct.
    Just did build.
    I know Editor has overhead, but did int expect that much of effect, which is a bit annoying.
    Ok, I could say case is resolved. (for now :) )
     
  23. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    We're working on improving that in the editor. Stay tuned!
     
    Lars-Steenhoff likes this.
  24. Xinzz

    Xinzz

    Joined:
    Jun 28, 2015
    Posts:
    67
    Same situation here, i'm using 2.2.0 too, and even if i set as FixedUpdate, jittery problem is still here.(shows on parallax background).
     
    krou likes this.
  25. krou

    krou

    Joined:
    May 29, 2014
    Posts:
    22
    The same problem with jittering still present in Cinemachine 2.3.3 in all Update Methods. I will be glad to any advice on how to fix the issue.
     
  26. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    Do you get this effect in the build, or just when running in the editor?
     
  27. krou

    krou

    Joined:
    May 29, 2014
    Posts:
    22
    Build also has jitters but less then in editor
     
  28. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    Is it possible that your target is being moved by your code in both FixedUpdate and Update/LateUpdate? That is a common cause for this sort of judder. The fix is to animate the target only in FixedUpdate or only in Update/LateUpdate.