Search Unity

Fixed camera follow stutter

Discussion in 'Scripting' started by ghiboz, Feb 28, 2020.

  1. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    hi all!
    I have this issue: I have a camera placed into a fixed position and I need that follow (only rotating) the player.
    I made this code:
    Code (CSharp):
    1.  
    2. var dir = target.transform.position - camera.transform.position;
    3. var wantedRot = Quaternion.LookRotation(dir, Vector3.up);
    4. camera.transform.rotation = wantedRot;
    5.  
    and works..
    now I wish to manage a smooth rotation, to avoid a rigid rotation between camera and target: so I made this:
    Code (CSharp):
    1.  
    2. var dir = target.transform.position - camera.transform.position;
    3. var wantedRot = Quaternion.LookRotation(dir, Vector3.up);
    4. camera.transform.rotation =
    5. Quaternion.Slerp(camera.transform.rotation, wantedRot, Time.DeltaTime * rotationDamping);
    6.  
    and it works, but the target stutter...
    I tried to place the code in the `LateUpdate`, in the `FixedUpdate`, but seems that `FixedUpdate` is better, but not completely solved

    I have an idea, but I don't know how to fix:
    should be that for example wanted rot is 10, and with Slerp I get a rotation of 4, in the next frame, the wanted rotation should be 12, but I start from 4, and so I made a Slerp with 8 and not 10 like before, and maybe the next frame is increased and this cause a stutter

    did someone help me?

    thanks in advance!
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    The stutter will depend on how fast the target is updating compared to your camera update. For example, if you update the target at 45fps and the camera at 30fps you will get a stutter effect.

    It should work if you update the target in Update() and update the camera in LateUpdate(). That means they are both updating at the same frequency but the camera is updating -after- the target has moved.
     
    niloxc likes this.
  3. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    thank you @tonemcbride !
    I had a problem on my game and I tried to test in a simple scene, using the `Cinemachine` samples,
    the update is on Update function, so should not be different

    and the `LateUpdate` seems to be the worst situation
     
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    If you have a test project you can post a link to it and I can have a look on Monday if you can’t get it working.
     
  5. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    If you're getting stutter, it will 99% be related to using LateUpdate() on the camera. If you're using
    transform.LookAt(pos);
    it will never get the pos in sync with Update()
    It sounds obvious, but stick any transforms for the camera right at the end of LateUpdate. ;)
     
  6. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    thank you for all...
    in my test seems that if I use LookAt works fine, the stutter appears when I try to smooth the movement with Slerp or Lerp

    here is the sample I made...

    move the character with wasd

    https://we.tl/t-G9orJ9IfM4

    thank you for all