Search Unity

Camera jitter problem

Discussion in 'Scripting' started by Headingwest, Dec 12, 2011.

  1. Headingwest

    Headingwest

    Joined:
    May 3, 2011
    Posts:
    153
    Anyhelp appreciated this one is driving me up the pole.

    I have a smooth camera following a skate boarder (rigid body, wheel colliders etc).

    Now if I lock the camera to the skater the camera is smooth, no jitter. But when I add the smoothed follow camera it jitter pretty badly. This is completely the opposite behavior that I would expect! Even worse I cannot stop it.

    Here is my camera code:


    Code (csharp):
    1.  
    2.  
    3.  
    4. float camlerpspeed = 2.0f;
    5.                
    6. camlerpspeed *= Time.smoothDeltaTime;
    7.  
    8. cameraPosition = playerTransform.position;
    9.                
    10. cameraPosition -= (playerTransform.forward * (cameraZOffset * 0.1f));
    11.  
    12. cameraPosition.y = playerTransform.position.y + (cameraHeight * 0.3f);
    13.                
    14. this.transform.position = Vector3.Lerp(this.transform.position, cameraPosition, camlerpspeed);
    15.                
    16. this.transform.LookAt(playerTransform.position);
    17.  
    18.  
    19.  
    I've tried storing the players last position adding them and dividing by two and lerping to that instead (from another thread) , with no luck. ive tried messing with ordering by moving my camera code to LateUpdate etc.

    The only thing that lessens the effect is reducing the camelerpSpeed variable but then the camera trails way too far behind the player!

    Any idea what is causing this jitter ?

    Thanks
     
    Last edited: Dec 12, 2011
  2. ufo_driver

    ufo_driver

    Joined:
    Jul 6, 2011
    Posts:
    93
    I encountered same problems when my camera was a child node of an object it was following. You can also put this code in LateUpdate(). Maybe this will help...
     
    borderlineart and Randomnation like this.
  3. Headingwest

    Headingwest

    Joined:
    May 3, 2011
    Posts:
    153
    Thanks for your reply , The camera has no parent nodes, I tried LateUpdate() as well with no observable difference.
     
  4. Headingwest

    Headingwest

    Joined:
    May 3, 2011
    Posts:
    153
    Ok if i set the lerp to 1.0f it becomes a static camera and is completely smooth. The lower I set the value the more it jitters. I still cant understand why this would be happening,
     
  5. jeffweber

    jeffweber

    Joined:
    Dec 17, 2009
    Posts:
    616
    I am having a very similar issue. If I try to use any sort lag/smoothing calc, the camera is jerky. Anyone have any ideas?

    -Jeff Weber
     
  6. Headingwest

    Headingwest

    Joined:
    May 3, 2011
    Posts:
    153
  7. ufo_driver

    ufo_driver

    Joined:
    Jul 6, 2011
    Posts:
    93
    You can try camera script from Unity's CarTutorial. If it will jitter then problem is somewhere else. But I have seen camera jitter bug in many samples and it looks like problem is more complex.

    Ah, I remembered one thing. There is Interpolate property in Rigidbody component. If I am correct setting it to Interpolate or Extrapolate will smooth object movements between different updates (not only fixed updates).
     
    Last edited: Dec 12, 2011
  8. Headingwest

    Headingwest

    Joined:
    May 3, 2011
    Posts:
    153
    I'm using interpolate as well as continuous dynamic collisions.
     
  9. angelonit

    angelonit

    Joined:
    Mar 5, 2013
    Posts:
    40
    I'm having the same problem here...
     
  10. maximus9600

    maximus9600

    Joined:
    Jun 9, 2014
    Posts:
    12
    I had a similar problem but LateUpdate worked for me
     
  11. Deleted User

    Deleted User

    Guest

    Do you move the skater with physics forces? If yes this could be the problem, as referencing a body that is beign forced by physics by its transform will result in this. I had this problem on my player look script for first person, I solved in one way, by adding a dummy object which is moved by my mouse script which only work out on the transform rotation of the camera and the dummy object that is child of the player.

    Then and only then I apply the force to the rigidbody that is dependant to the dummy object direction, all this with Interpolation on and I get smooth movement with and without v-sync. In your case this would be a little more complicated as is in third person for what I understand and you need to work out some more on it.
     
    StarManta likes this.
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I bet you that the issue is that FixedUpdate is not running consistently every frame. In other words, for each frame, your skater goes from:
    (1,0,0)
    (1.2,0,0)
    (1.2,0,0)
    (1.4,0,0)
    (1.4,0,0)

    an so on. So your camera (which runs every frame), moves closer to your skater every frame, which is staying still for half the frames.

    I might move the camera code to FixedUpdate rather than LateUpdate and see what happens. You may have to adjust the script execution order so that the camera script happens after "default time".
     
    Orion336, LarV123, beingbat and 3 others like this.
  13. Cortex99

    Cortex99

    Joined:
    Oct 3, 2014
    Posts:
    1
    I'm experiencing this issue as well and I couldn't find a solution that I understand in this thread. Could anyone give me a clear explanation on how to fix this issue?
     
  14. Gapp

    Gapp

    Joined:
    Oct 30, 2013
    Posts:
    2
    It seems to be linked to what you are tracking with the camera as to when to update the position. LateUpdate works well for most systems but I found if you are tracking physics objects which are updated in FixedUpdate then you also want to update you camera positions in the FixedUpdate function.
     
  15. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    Good explanation... Helped me.

    What worked for me is, if the object I am following is not physics based object, camera goes into LateUpdate(). If the object I am following IS physics based, then the camera goes into FixedUpdate().
     
  16. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    384
    For anybody searching for answer this is it. You camera has to be in same update as the object you are following. So when player uses physics and updates in FixedUpdate() put your camera movement code into FixedUpdate() as well
     
  17. superme2012

    superme2012

    Joined:
    Nov 5, 2012
    Posts:
    207
    I have tested this, one thing to note is that (as it seems), the smooth results may only be visible in a build. And another thing I noticed is that movement seems to have a slight pull/wobble at times (if in fixed update), I'm assuming it could be down to frame rate (not slow but fast frame rate between 60 and over).. Not tested but capping frame rate may remove the pulling/wobble effect.
     
    Last edited: May 14, 2016
  18. gonzooin

    gonzooin

    Joined:
    Jan 21, 2016
    Posts:
    1
    I had a similar problem that get fixed by using FixedUpdate(). thanks !
    For the record, here my story : I had a player that uses physics and the camera position was tie to the player. Everything was fine until I tried to move other objects in the same time I move my player. The other objects seems to be always going back and forth and they are chasing the player. I just move the code in FixedUpdate to fix it.
     
  19. TriBackBill

    TriBackBill

    Joined:
    Feb 6, 2017
    Posts:
    1
    One way that I found worked well was to, rather than controlling the camera directly, control a gameobject with the camera as a child, on which is a rigidbody with interpolation enabled. As well as this, I enabled extrapolate on my character. This might work for you.
     
  20. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    So is the bottom line of all this that the physics system just doesn't work well with Kinematic objects? If you want your game to look professional, you either have to make every object in the game including the camera update in FixedUpdate, or else you have to do everything 100% kinematically?
     
  21. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    I have only noticed an issue with the camera and move it to fixed update. If your objects are jittering then turn on interpolation on the rigidbody.
     
    AIE_Software likes this.
  22. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    When I do that, the result is wrong on about 20% of spawned rigidbody projectiles.
     
  23. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    What do you mean by wrong?
     
  24. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    I mean even though the projectile is rotated correctly, it shoots off in an arbitrary direction, instead of the new direction I just applied to it. (My projectiles use object pooling, and there are a lot of them alive at once, so I think we might be seeing old velocities because the physics update is timing out or something.)
     
  25. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Never seen that before. How and where are you setting the velocities?

    Again. How and where are you setting the velocities?
     
  26. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    In the projectile:
    Code (CSharp):
    1.  
    2.    void OnEnable () {
    3.        rb.velocity = new Vector3(0f,0f,0f);
    4.        rb.angularVelocity = new Vector3(0f,0f,0f);
    5.        lifetimeSpent = 0.0f;
    6.    }
    7.  
    8.     //void LateUpdate(){
    9.        //Vector3 p = transform.position;
    10.        //p.y = 0f;
    11.        //transform.position = p;
    12.    //}
    13.  
    In the shooter:
    Code (CSharp):
    1.                 var obj = prefab.Get<Projectile>();
    2.                 if(obj != null){
    3.                     obj.transform.position = (turret.position + turret.forward) - (turret.forward * timeSinceLastShot * bulletSpeed);
    4.                     obj.transform.rotation = turret.rotation;
    5.                     obj.gameObject.SetActive(true);
    6.                     obj.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
    7.                     obj.gameObject.GetComponent<Rigidbody>().AddForce(turret.forward * bulletSpeed, ForceMode.VelocityChange);
    8.                     obj.gameObject.transform.parent = playerShotHolder;
    9.                 }
    10.  
    Edit: Can't remember why I put the LateUpdate stuff in there since I also locked the Y axis on the rigidbody. Commented that out. The problem's still there, but I noticed I can't seem to take screenshots of it. I'm begining to suspect it might be an optical illusion caused by screen tearing/monitor refresh issues. When I take a screenshot, the shots look perfectly placed. When I'm playing the game they seem to split into two overlapping streams if I move diagonally.
     
    Last edited: Mar 22, 2018
  27. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    If vsync is enabled try disabling it. If its disabled enable it. Its enabled by default, usually vsync fixes tearing issues. But this will allow you to see if it is the problem. Your code looks fine so I don't see why it would do that.
     
  28. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    vsync had no effect.

    I managed to take a screenshot of it. I think I had trouble capturing it before because it doesn't happen every single shot.

    This is a constant stream of projectiles moving pretty quickly.
     
  29. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Okay after digging deeper I think I've figured out the problem. My time between shots was .01f. By increasing it to 0.0625f I managed to get the problem to be invisible, but now my ship doesn't fire as many shots at a time which is less than ideal.

    I think the cause is something to do with Update being out of step with the physics system, resulting in shots being fired even though the previous shot still hasn't moved yet. This is why it's only visible when moving diagonally (player ship moved on Update but bullet moves on FixedUpdate) or when quickly turning the turret (turret rotates on update but previous shot moves on FixedUpdate)

    So I ask again, is there any way to fix this, other than moving everything in my entire game to FixedUpdate?
     
  30. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Okay, I went into Edit > Project Settings > Time and changed the Fixed Timestep from 0.02 to 0.01, and changed my player ship's fire rate to 0.02f. I figured if the timestep is twice as fast as the script is pulling the trigger, every bullet must have at least one FixedUpdate.

    I also added a second bullet, which is something I was going to eventually do anyway, given the twin-barrel design of the ship's turret. This makes up for the fact that I'm firing bullets half as frequently in terms of rebalancing the whole game's damage values.

    The result is mostly okay.



    There's some visible inconsistency in the spacing between the bullets. I guess this is due to the small amount of time that has passed between the last physics update and the current Update. There might be a way to mathematically correct for this, but I don't know what it is.
     
  31. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    @WarpZone We should move this to its own thread, we have hijacked this old thread. So please start a new thread and @ me so I can respond.
     
  32. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Diversion continued in this thread. Because I know from past experience, Google will send people interested in this subject here, not to the Continuation. Google's stupid like that. Gotta cross-reference the entire internet as you go, otherwise it's unreadable by people living in The Future.
     
    BenMacTavish likes this.
  33. pbakker

    pbakker

    Joined:
    Apr 27, 2017
    Posts:
    1
    Since fixedUpdate doesn't always run at the same rate, you should multiply your movement vectors by
    Time.deltaTime
    . This worked for me.
     
  34. mackhack2015

    mackhack2015

    Joined:
    Aug 26, 2018
    Posts:
    3
    I am using the cinemachine camera for my follow but it still does the same thing. I fixed the jitter by putting the update mode in the cinemachine brain to fixed and the interpolate to interpolate in the rigid body
     
  35. TeacherSteven

    TeacherSteven

    Joined:
    Jan 5, 2018
    Posts:
    1
    This totally worked for me. Making sure both were using FixedUpdate instead of one using LateUpdate and one using FixedUpdate. Thank you!
     
  36. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    This... doesn't make any sense. The GPU isn't involved in physics calculations and a high framerate does not impact physics either. Physics runs on a fixed timestep that is not impacted by the in-game framerate. No amount of movement ever gets "the full power of your GPU" because your GPU doesn't do anything with movement.

    That sounds like you have a badly programmed cube or a badly programmed camera. If you have problems that arise from the framerate being too high, your code is framerate-dependent and that's a problem for you to fix, not a Unity bug.
     
  37. Orion336

    Orion336

    Joined:
    Feb 17, 2019
    Posts:
    2
    We're working on our first game and our player (a ball) is moved along the scene with quite high velocities and jitter is a huge issue. We use an addForce to make it more fluent and do that in the FixedUpdate along with all of the other physics, we now moved the camera to update in the FixedUpdate too and yes, we might be 1 frame behind, but at least it's consistant and there's no issue of jitter at all anymore. Thank you for this solution, it's something logical but easy to be overlooked when you're further in the project.
     
  38. dasraiser

    dasraiser

    Joined:
    Sep 28, 2012
    Posts:
    6
    if the camera transform is being calculated before or after the skater, one of the transforms will always be out of sync.

    pass the skaters position to a variable inside the camera script after you update the skaters position and use that to calculate the camera.
     
  39. mwendaenrique5

    mwendaenrique5

    Joined:
    May 10, 2020
    Posts:
    3
    Late Update Worked for me. I wasn't using any Rigidbody though. So not a physics issue.
     
  40. jpwrunyan_unity

    jpwrunyan_unity

    Joined:
    May 14, 2020
    Posts:
    1
    My problem was completely different from the above. I was updating my camera's position to follow a 3rd person object on screen in LateUpdate. If I tried rotating the camera up/down *after* updating the position, it was jittery. But if I switch the order of the lines so that it would rotate the camera up/down *before* updating the camera object's position, the jitteryness is gone or at least not noticeable. I did not have any jitteryness with the y-axis (left/right) rotation, but it was rotating around the 3rd person object. The up/down rotation was rotating using the main camera's transform.

    It's very strange that this made the difference, but I'm glad it did and hope it might help someone else.

    tl;dr changing my code to change camera rotation *before* camera position fixed my issue.