Search Unity

Camera Jitter, depending on the settings either the player or the environment jitters

Discussion in 'Cinemachine' started by SirWhiteBeard, Feb 4, 2020.

  1. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    Hi there,

    I have reworked my player controller to be rigibody based and eversince I am experiencing jitter on either the player or the environment. This is especially noticeable when playing in build. I have made a menu to be able to toggle v-sync on and off and when running my build with v-sync off I have approximately 350fps. With the current settings you will notice there will be quite some background jitter. I have tried changing the damping settings, which fixes the background jitter mostly, but causes the player to jitter. I have also changed the update method to FixedUpdate, but this makes the jitter visable with both v-sync turned on and off. I'm running out of options and have no clue where to tackle this problem. I hope someone can help me out!

    For convenience I have added my PlayerController Script and screenshots of the settings on my CinemachineBrain, CinemachineFreeLook and Cinemachine Collider. I'm using Unity 2019.3.0f6 and updated to the most recent version of Cinemachine.

     

    Attached Files:

  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    Ususally "either the player of the background jitters" is a symptom of uneven animation. Your character is not moving smoothly, and if that's the case then it's impossible to have a camera show both a smooth background and a smooth player at the same time.

    The solution is to fix your player animation. If it's a rigidBody, make sure all animation is happening in FixedUpdate, never in Update. Maybe try moving the logic in your script from Update() to FixedUpdate().
     
  3. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    Hi I understand the problem I think. The movement and it's animations however are already done in FixedUpdate. It's only the jumping that is done via Update as far as I can tell. And is everything is done in FixedUpdate does the CinemachineBrain need to be set to Fixed Update aswel?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    If everything is done in FixedUpdate, then CM Brain should be set to that also. If you set it to SmartUpdate, it will auto-detect how the target moves and report its findings in the vcam inspector when the game is playing. It's helpful for debugging.

    upload_2020-2-4_15-24-15.png
     
  5. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    Thanks for the headsup regarding the debugging. It is indeed saying FixedUpdate. I have now set the CinemachineBrain Update method to FixedUpdate. Unfortunately I still have the jittering occuring, now even with v-sync turned on. I have tried playing all code of my PlayerController within fixed update, which causes some minor bugs (Jump input unresponsive, which is expected) but the camera still jitters. I will try to make a recording for a more visual representation of the problem.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    With everything on FixedUpdate, does the jitter happen in the build? Does it happen in the Editor when the inspector is not visible?
     
  7. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    With everything in FixedUpdate it happens in both the Editor with only gameview visible and in build on with both v-sync on and off. I have tried creating a video of the issue, but it's hardly noticeable because of compression. None the less here is a link:


    Edit: it is especially noticeable at the yellow ship and it's light aswel as the trafficsign on the ground when the player/camera gets closer.
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    You're right - I can't really see it in the video.

    Maybe it has something to do with the aliasing between physics framerate and render framerate. Have you tried adding Interpolation to your player RigidBody? It's designed to smooth such jitter.

    If you do, then you'll need to put the brain in Smart or Late update. Fixed will jitter, as Interpolate will modify the player's transform in Update.
     
  9. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    I had already tried that unfortunately without any luck. It's one of the solutions I came across when first googling that issue. I'd happily share a build or the project if you want.
     
  10. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    Small little update, the problem seems to occur in older builds to 2018.3 and something else I noticed, is that the problem seems to become worse over time and won't be there initially.
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    Can you use 2018.4? That's our LTS version and we don't really support any of the other 2018 variations
     
  12. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    I could download 2018.4 and give it a check, but the issue was present in both 2018.3 and 2019.3,
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    oh, I understood from this that the problem was only in builds older than 2018.3
     
  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    Do you get the accumulating jitter if you remove Cinemachine and parent the Camera directly to the player?
     
  15. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    I will give it a shot, if the problem persists than I should see the same background jitter right?
     
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
  17. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    I have disabled the CinemachineManager and parented a new Camera tagged MainCamera with the Post Processing Stack V2 to my player and while I don't have the option to freely look around there is no jitter even after playing for about 5 minutes. With Cinemachine it would start jittering more and more after about 1 minute.

     
  18. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    If you could share a minimal project that reproduces this effect and send it to me I could take a look
     
  19. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    I'm currently at work, will do so when I get home. Do you have any preference on how or where I upload it? Thank you very much for the support thusfar!
     
  20. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    Last edited: Feb 5, 2020
  21. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    Hi @Gregoryl
    After doing some more experiments with a more barebones project I still get the stuttering unfortunately. This project might be more suitable to take a look at.
     

    Attached Files:

  22. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    Thanks for the upload. I ran the scene and moved the character around but am unable to see any jitter. Is there anything specific I should be looking for, or any specific action I need to do?

    One thing I noticed: you had Interposlation on in the player, but the Brain Update was set to fixed. For now, leave the Brain on Smart Update. As you turn Interpolation on and off on the player, the Brain will figure out the appropriate update mode for the vcam (which for Interpolate is Late, otherwise Fixed).
     
  23. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    The interpolation should be turned off, but I might have forgotten to turn it off again after experimenting with the project. Which project did you test if I may ask, or did you test both? To see the jitter occuring the fps needs to be high 200+ (v-sync needs to be turned off) and move form left to right or right to left, like in the image. You'll see the blocked stairs jittering from left to right. In the larger project, which I uploaded to google drive it's the same case (when v-sync is turned off and you achieve 200/300 fps) make sure to build the project and walk away as far as possible from the rock with the ship on it, try changing the camera angle and eventually the jittering will start happening. It's really hard to record the jittering since recording will also impact the fps, but I'll give it another shot.



    Edit: I finally managed to record the jitter
     
    Last edited: Feb 7, 2020
  24. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    Camera judder will certainly be caused by uneven movement of the character. To ensure even movement, I did 2 things in your RigidBodyChcarcter.cs: moved all the code to FixedUpdate, and added _inputs.Normalize(). That way, you're for sure moving the character at a fixed speed.

    Then, I see no judder at all, with or without interpolation, and with or without Cinemachine, running at >400 fps.
     
  25. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    I will give that a shot, would you mind sharing the code you changed exactly? I'm not sure what you mean by _input.Normalize(). And doesn't moving the input to the FixedUpdate cause it to be unresponsive, since it's not checking if there is input every frame?
     
  26. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         _isGrounded = Physics.CheckSphere(_groundChecker.position, GroundDistance, Ground, QueryTriggerInteraction.Ignore);
    4.  
    5.         _inputs = Vector3.zero;
    6.         _inputs.x = Input.GetAxis("Horizontal");
    7.         _inputs.z = Input.GetAxis("Vertical");
    8.         _inputs.Normalize();
    9.         if (_inputs != Vector3.zero)
    10.             transform.forward = _inputs;
    11.  
    12.         _body.MovePosition(_body.position + _inputs * Speed * Time.fixedDeltaTime);
    13.     }
     
  27. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    Hi @Gregoryl did you have a chance to look at this project? Unfortunately I'm still not having any luck with fixen the jitter in mij project. I have changed various settings and thusfar the only thing to fix it is lowering the fixedtimestep to 0.01, which shouldn't be necessary. I was also wondering what the setting does form the image below, since it is minimizing the jitter although it's still present.



    On a side note, here is some code of how I'm currently handling my movement, but still without any luck.

    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.         // Input
    4.         if (GroundCheck() && !rotationBlock)
    5.         {
    6.             inputDir = input.normalized;
    7.             RunAxis();
    8.         }
    9.         else
    10.         {
    11.             inputDir = new Vector3(0, 0, 0);
    12.         }
    13.  
    14.         if (pushControllerScr._pushBlocker)
    15.         {
    16.             input = Vector3.zero;
    17.         }
    18.         else
    19.         {
    20.             input = new Vector3(InputManager.MainHorizontal(), 0, InputManager.MainVertical());
    21.         }
    22.  
    23.         // Makes the player able to move, while not landing or being dead
    24.         if (!landState && !climbManagerScr.isClimbing)
    25.         {
    26.             Move(inputDir, running);
    27.         }
    28.     }
     
  28. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    What does Move() do?
     
  29. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    Can you prove that your character is moving smoothly? Maybe output its velocity each frame and dump it to a spreadsheet and graph it? If the movement isn't smooth then no amount of camera tinkering will fix your problem.
     
  30. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    This is what move does:
    Code (CSharp):
    1. if (inputDir != Vector3.zero)
    2.         {
    3.             float targetRotation = Mathf.Atan2(inputDir.x, inputDir.z) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
    4.             transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
    5.         }
    6.  
    7.         float targetSpeed = ((running) ? runSpeed : movementSpeed) * inputDir.magnitude;
    8.  
    9.         stateInfoBaseLayer = _anim.GetCurrentAnimatorStateInfo(0);
    10.  
    11.         if (stateInfoBaseLayer.IsTag("Climbing") || stateInfoBaseLayer.IsTag("Standing"))
    12.         {
    13.             currentSpeed = 0f;
    14.         }
    15.         else
    16.         {
    17.             currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
    18.         }
    19.  
    20.         if (stateInfoBaseLayer.IsTag("AirIdle") && currentSpeed > walkSpeed)
    21.         {
    22.             currentSpeed = walkSpeed;
    23.         }
    24.  
    25.         velocity = transform.forward * currentSpeed + Vector3.up * _rb.velocity.y;
    26.  
    27.         if (_capsuleCol.enabled)
    28.         {
    29.             _rb.MovePosition(_rb.position + velocity * Time.deltaTime);
    30.         }
     
  31. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    I'm unsure how I would be able to do that? Do you mean debug logging the velocity every frame and putting them into a spreadsheet? the stange thing is that the jittering at 300 fps+ is coming and going when walking around. If you walk far away from the yellow ship you can clearly see it jittering from left to right for like 10 seconds and when stopping and walking and changing direction it can go away aswel. Then after a while the jittering comes back. This can only be noticed in build, since in editor I won't achieve 300 fps. I would really appreciate it if you could take a look at the bigger project since I can't seem to recreate the exact scenario in a new project.
     
  32. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    Code looks ok to me. If you can prove a smooth velocity then we can lay this question to rest and look again at the cameras.

    I looked only at the last simplest project you posted, not at the intermediate one. I might be able to check out that one a little later.

    The Blend Update Method controls when the active vcam's settings are blended with the outgoing vcam's settings, and applied to the main camera. I'm surprised it makes any difference to the jitter, since no vcam blends are involved.

    Yes, log to a spreadsheed and graph. Try to do it only when you see the jitter. Note that it might also be rotational jitter. Are you sure that it's not just because of pixel aliasing in the render of distant objects?
     
  33. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    I understand.

    If you could check out the intermediate one that would be amazing, since it's quite difficult to describe the problems without being able to capture them.

    That's what I was thinking, initially I wasn't even planning to experiment with this setting because of that reason.

    In all honesty I have no clue on how to log the Debug.logs to a spreadsheet or graph. Do I have to do this manually? The jitter also only happens in a standalone build, since this is the only way to achieve 300+ fps on my pc. I'm quite sure it's not pixel aliasing since the jumps from left to right are quite big. Tommorow I will give recording it another try, this has to be filmed by my phone however, because when I'm using recording software the fps in my game drop to a point where the jitter isn't nearly as bad.
     
    Last edited: Feb 10, 2020
  34. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    @Gregoryl I have been able to get an output_log.txt, which shows the velocity of my RigidbodyController script. Is there a way to convert this to a spreadsheet/graph? There is quite some irrelevant stuff between every frame in the output_log.txt.
     
  35. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    Hi @Gregoryl just got back from work and I finally managed to get a descent capture of the jittering. Note that this is not all the time, it comes and goes while playing (i.e. rotating the player and walking in other directions might stop it, and it will eventually come back). I also managed to get a dump of the game while playing and made it as readable as possible. It shows the rb.velocity, which only changes on the y when the player jumps (Addforce is used) and the custom velocity which is used to translate the input to the movement (rb.MovePosition). The dump looks clean to me without any jumps between frames.

     

    Attached Files:

    Last edited: Feb 11, 2020 at 7:52 PM
  36. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    Small little update on the project, someone gave me the tip to create my own custom camerafollow script to check if the jitter/stutter is still present. This custom camerafollow script didn't come with any jitter, so it probably has something to do with Cinemachine. The script I'm using is as follows;

    Code (CSharp):
    1. void FixedUpdate() {
    2.     yaw += InputManager.SecHorizontal() * mouseSensitivity;
    3.     pitch -= InputManager.SecVertical() * mouseSensitivity;
    4.     pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);
    5.  
    6.     currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
    7.     transform.eulerAngles = currentRotation;
    8.  
    9.     transform.position = target.position - transform.forward * dstFromTarget;
    10. }
    So this solved the jitter, but I would really like to use Cinemachine and Timeline in my project, which unfortunately still has the jitter/stutter problem. For this reason I figured I should delete Cinemachine from my project and re-install it (I have been working on this project for about 2 years) and check if that solves it. Unfortunately it didn't, but this made sure I had to rebuild the Freelook camera, the Cinemachine Collider and Cinemachine Brain within my project, so I had a look at all the settings again and did some testing. I eventully found out that the x, y, z damping on the top, middle and bottom rigs caused the major jitter/stutter so that's currently on 0 instead of the standard 1. Like I said this fixes the major horizontal jitter you encouter every now and again while playing, but there is still some jitter which doesn't happen when using my own custom camerafollow script. This seems to be rotational jitter/stutter, but the stutter seems to be there all the time and unfortunately I haven't found a fix yet.
    I have changed my PlayerController script a little bit as you can see below;

    This is my new rotation code:
    Code (CSharp):
    1. float targetRotation = Mathf.Atan2(inputDir.x, inputDir.z) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
    2. targetRotation = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
    3.  
    4. Quaternion targetRot = Quaternion.AngleAxis(targetRotation, Vector3.up);
    5.  
    6. _rb.MoveRotation(targetRot);
    This is my movement code:
    Code (CSharp):
    1. velocity = transform.forward * currentSpeed + Vector3.up * _rb.velocity.y;
    2.  
    3. if (_capsuleCol.enabled) {
    4.     _rb.MovePosition(_rb.position + velocity * Time.fixedDeltaTime);
    5. }
    Note that this runs in FixedUpdate, which it should.

    Like I said this al leads me to believe the jitter/stutter is caused by Cinemachine, since my movement seems clean/smooth and the jitter/stutter doesn't occur with my custom camera follow script. Below are the settings I currently have on the Rigidbody, Camera and Cinemachine Freelook:









     
    Last edited: Feb 15, 2020 at 9:40 PM
  37. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    38
    Another small little update, the main cause of the "big jitter" that occasionally happens with v-sync off (300+ fps) is the x, y, z damping on the top, middle and bottom rigs. Turning the standard "1" damping down to 0 "fixes" this.



    Now I'm still encountering constant jitter with the settings mentioned in my previous comment, so I thought going back through the "fixes/suggestions" I had already tried since the situation is a bit different now that I know the damping was causing the major jitter/stutter.
    Eventually I came back to the Cinemachine Brain, Blend Update Method, which is set LateUpdate by default. I first want to demonstrate the custom Camera Follow script I had created for testing, which you can see below:


    Custom Camera Follow script used (No Cinemachine)

    And below are two recordings of the same segment with the Cinemachine Brains Blend Update Method set to LateUpdate (default) and FixedUpdate.


    Note that there is severe jitter while running around the yellow cube. The Blend Update Method is at it's default LateUpdate.


    Here the Cinemachine Brain is set to FixedUpdate the same as the Update Method and all of my movement is also handled in FixedUpdate. And the jitter seems to be gone as far as I can tell.

    Now the question is, like I said in one of the earlier comments I thought I wasn't using any blending, since I'm using only one camere (my main camera). Why does this fix it? And I couldn't find much documention regarding the Blend Update Method, since it seems to be added recently? If someone from Unity could elaborate that would be amazing! What is this Blend Update Method used for?

    Another thing I encounter is alot of misinformation regarding putting your CameraMovement in either Update, FixedUpdate or LateUpdate. The Cinemachine Brain only has 3 options, FixedUpdate, LateUpdate and Smart Update and I think SmartUpdate checks what kind of movement there is and decides whether to use Fixed or LateUpdate. As of now, all my settings are set to FixedUpdate, which also handles my Rigidbody movement (MovePosition and MoveRotation), but I have also been told to strictly use LateUpdate for any camera movement. I hope someone could answer this for me aswel.
     
unityunity