Search Unity

Cinemachine stutters when following a Rigidbody

Discussion in 'Cinemachine' started by Vallar, Jan 25, 2020.

  1. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    Hey,

    I have an object that is being moved by setting its velocity. The Rigibody doesn't have interpolation set. Meanwhile I have a Cinemachine Virtual Camera set to Transposer (Lock to Target No Roll) with some dampening applied and follow offset. The Aim is set to Hard Look At

    Now my issue is that the camera is stuttering quite a bit. At first I thought it was the object itself but when I switch to the scene view, the object is moving smoothly and the camera seems to jump around.

    I tried to switching update modes on Brain to various combination but it didn't help. Same with Standby Update.

    Any idea what can I do to get it to smoothly follow its target without this stutter?

    Thanks.
     
    jcameron47 likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Standby Update is not relevant here.

    If you set the Brain's Update Method to Smart Update, then play the scene and look at the virtual camera's inspector while the game is playing, what text do you see here?

    upload_2020-1-27_13-34-58.png
     
  3. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    It says Fixed Update

    Edited: corrected the update method it was using.
     
    Last edited: Jan 28, 2020
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    It says FixedUpdate? And it's stuttering? That should not be, unless your target is moving unevenly. But you say it's smooth.

    Can you make a small project the reproduces this problem, and send it to me?
     
    emre_ozay likes this.
  5. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    That is what I see in the scene, it moves smoothly. Can I send the project (nothing more than the movement) in a PM please because it uses some paid assets from the store?

    Thanks.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Sure. Delete the library folder before zipping, it will be much smaller
     
  7. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    Thanks. Done and sent.
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Had a look at the project, thanks for uploading.

    I'm definitely seeing some stutter when playing in the editor. I believe that this is due to the discrepancy between the physics framerate and the render framerate. You will get aliasing, which is magnified by 2 things:

    1. Running in the Editor, which has a lot of extra overhead, making the render framerate more uneven. To get a more accurate idea of the situation, you should test a build of the project. When I do that, the stuttering is very considerably less.

    2. Using Damping on the camera. Unfortunately, damping is very sensitive to uneven frame rates, and the stuttering will be amplified by this. One way to mitigate this is to reduce the aliasing by increasing the physics framerate. When I changed the Fixed Timestep (in ProjectSettings/Time) to 0.01 it was quite smooth in the build.

    On a side note, changing the vcam's Aim to Composer (with default values) made the camera very sweet.
     
  9. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177

    Thanks a lot for the information. I think I'll go with the Composer for now given it feels very smooth and good. But is there anyway to fix this in editor? It is very annoying to test with this jumping behavior the camera has.

    That said, how can the editor mess up the smoothing so much? Nothing drastic is running that it takes so much time to render things or am I misunderstanding what you meant?
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    A shorter fixed timestep definitely helps in the editor too. Don't make it too short, or you'll kill performance.

    The editor is a big complex program and it does lots of stuff, even if there's not much in your scene. The build, on the other hand, is 100% focused on the game.

    Smooth camera movement with an uneven framerate is a hard problem: because it's the camera, any little glitch in its smoothness results in a hugely visible artifact, which our human brains are very much tuned into detecting. Putting some effort into guaranteeing a smooth framerate would be a rewarding investment.
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Update: I thought of something this morning and had another look at your project.

    Your player is being driven on the physics clock, but your script is tinkering with the velocity on the render clock. That's a problem. So I changed PlayerInput.Update() to PlayerInput.FixedUpdate(). Then, everything was perfectly smooth in the editor, even with the physics timestep at its original value of 0.02. Setting the timestep smaller was just covering up the real problem.

    You can even increase the timestep further and, if you want, you can enable rigidbody interpolation on the player to smooth it out. It all works quite well.

    The take-home message is that you have to be very careful with physics-driven objects to consistently alter the movement only in FixedUpdate, never elsewhere. Otherwise you're liable to get judder.
     
    Last edited: Jan 29, 2020
  12. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    I was going to post about it being smooth when I was moving the object using Transform.Translate then I thought, maybe this is an Input issue given I forgot to change that part. Then I came in here to say this only to see you've spotted it. Thanks a lot for the heads up.
     
  13. suibriel

    suibriel

    Joined:
    Jun 20, 2013
    Posts:
    4
    Hey great thread guys - helped with something I was doing too xx!
     
    gooseguava likes this.
  14. kkaehler

    kkaehler

    Joined:
    Nov 15, 2016
    Posts:
    11
    Agreed, incredibly useful :) In my 2D project, moving the RigidBody2D in FixedUpdate() and turning on interpolation did the trick. Thanks!
     
  15. unity_zL4mJaTePZ9bOg

    unity_zL4mJaTePZ9bOg

    Joined:
    Feb 1, 2020
    Posts:
    1
    Thank you so much! This fixes all my problems! And prompts to you for making the Cinemachine, my brother! I learned a lot by reading the source code of the cinemchine :)
     
    gooseguava and Gregoryl like this.
  16. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Sorry to necro, but i can't figure this out. It says "Fixed update"but it stutters a lot on vertical movement. I tried turning off the old script i had that was altering the camera's position as well as freezing the player's movement, but it still stutters. Pretty sure i'm not taking camera input from any other scripts, it's strange...

    Did i set one of these settings wrong? If i change the decel/accel time it doesn't seem to do anything.
    upload_2023-2-28_18-1-19.png
     
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    @Mashimaro7 What is the Update Mode setting of the CM Brain?
     
    Mashimaro7 likes this.
  18. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Oh, i didn't realize there was settings for that on there. It was smart update, i changed it to late and it fixed the stutter, thanks!
     
    Mangonels likes this.