Search Unity

[SOLVED]Camera jitter as soon as using Lerp

Discussion in 'Scripting' started by Craze74, Oct 16, 2019.

  1. Craze74

    Craze74

    Joined:
    Nov 19, 2012
    Posts:
    83
    Hello everyone !

    I checked a lot of existing posts, tried a lot of things but could not figure it out.

    I am moving an object using character controller, my camera is following it and looking at it that way :

    Code (CSharp):
    1. transform.position = player.position;
    2. Quaternion lookRot = Quaternion.LookRotation(player.position - transform.position);
    3. transform.rotation = lookRot;
    This is in late update, and I am moving my object in Update.

    This is working fine, but as soon as I am trying to smooth out the movement using a Lerp, camera begins to jitter.
    From what I understood it seems it is related to the Lerp function ( OR that we should not be using a point that is moving as the first parameter ? ), and since I am pretty new to dealing to camera movement, I am not sure how to do things right to fix it correctly without surcharging and adding non necessary stuff..

    I tried using the lerp like this :

    Code (CSharp):
    1. Vector3 smoothMov = Vector3.Lerp(transform.position, position, Time.deltaTime * 30f);
    2.             transform.position = smoothMov;
    I could try to use SmoothDamp, but I also saw that it was more processing time consuming than a Lerp


    Thanks !
     
    Last edited: Oct 16, 2019
  2. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Hi!

    Try using a value like 0.1f on the alpha parameter of the Lerp instead using Time.deltaTime * 30

    Hope it helps, cheers!
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah your use of "Time.deltaTime * 30f" is a problem. It should be a percentage of the full movement expressed as a value between 0f and 1f.

    Generally you'd have two points you are moving between over several frames as you increase that percent value each frame. Though changing those points every frame like it looks like you're doing is going to make it difficult to smoothly lerp between them.
     
    Giustitia likes this.
  4. Craze74

    Craze74

    Joined:
    Nov 19, 2012
    Posts:
    83
    ooh I see...

    So basicly we should never use Lerp for camera interpolation, but rather smoothDamp ? because even if I am having a value going from 0 to 1, the initial position we are initiating the interpolation changeanyway, so it's absolutely useless in that situation

    I saw this in many tutorials being used :S
     
    ivankiran25 likes this.
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Is your camera following an object that is moved with physics? If so, that is the source of the problem, and the camera should run in FixedUpdate. See last section here.
     
  6. Craze74

    Craze74

    Joined:
    Nov 19, 2012
    Posts:
    83
    Hello !

    No I am not using any rigidbodies or physics, just a character controller


    I just did some tests with smoothDamp, does not looks like a viable alternative than Lerp, still doing some weird jitters somehow
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    upload_2019-10-17_10-26-30.png

    You're using physics.
     
  8. Craze74

    Craze74

    Joined:
    Nov 19, 2012
    Posts:
    83
    :O but I thought FixedUpdate was only when using a rigidbody component and actually applying physics forces to make the object move :O
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Well try doing it in FixedUpdate (it's a 5 second fix) and see if it works.
     
  10. Craze74

    Craze74

    Joined:
    Nov 19, 2012
    Posts:
    83
    Putting the camera in Fixedupdate() and also the movement of the object in Fixedupdate(), seems it fixed the jitter indeed... I do need the object movement also in Fixedupdate right ?

    In any way thanks a lot for the explanation, that makes more sense
     
  11. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    FixedUpdate function is used to do something on the physics update. You can do whatever logic you want in there, it doesn't have to be specifically physics operations with a rigidbody. But since we know that the character works with physics, then we also want to move the camera to face the character every time the physics system updates.

    https://docs.unity3d.com/Manual/class-CharacterController.html
    This means that the character controller does not use a Rigidbody to move, but still uses the physics system to constrain it and collide with objects.
     
  12. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    You can move your CharacterController in Update with no problems at all if you are not moving it using forces but using his Move or SimpleMove functions.

    Yes, you can use Lerp for camera interpolated movement. If you use your current position as start position of the lerp and a constant alpha value (like 0.1f) what you get is a smoothed movement with ease out. Thats because you are moving your camera to the target position a 10% of the distance on each call. So as you get closer to the target, that 10% becomes a smaller value resulting in that "ease out" curve.

    So, if you move your character in Update (dont forget multiplying your speed value * Time.deltaTime!) and your camera in LateUpdate everything must look fine :)

    Hope it helps, ask me if you still get problems!
     
  13. Neeraj_Kumar

    Neeraj_Kumar

    Joined:
    Apr 13, 2019
    Posts:
    1
    Just solved it by changing Update() to FixedUpdate(), and Time.deltatime to Time.fixedDeltatime
     
  14. pikachu_is_op

    pikachu_is_op

    Joined:
    Feb 3, 2022
    Posts:
    1
    Some other possible fixes include:

    1. Changing the CinemachineBrain's update method from "SmartUpdate" to "FixedUpdate."

    2. Changing the Damping settings on the CinemachineVirtualCamera.
     
    Last edited: Jan 7, 2023