1

I am seeing a camera stutter when using smooth follow to lerp after my player with my camera in my multiplayer browser game. The player position is received from the server, the player lerps to that position, and my goal is for the camera to smoothly follow the player with its own, extra smoothing.

You can find my game at http://orn.io to see the current state, without smooth follow camera lerping, which is better but causes choppy movement and creates a headache. The current code for camera follow is:

void LateUpdate ()
{
    if (Target == null) {
        return;
    }

    currentScale = Mathf.Lerp(currentScale, -GameManager.TotalMass, Mathf.Clamp01 (Time.deltaTime * heightDamping));

    Vector3 h = currentScale * 2f * Target.up;
    Vector3 f = currentScale * 3f * Target.forward;

    // tried lerping to this value but it causes choppy stutters
    Vector3 wantedPosition = new Vector3(Target.position.x, Target.position.y, currentScale * 2) + h + f;

    myTransform.position = wantedPosition;

    myTransform.LookAt (Target, -Vector3.forward); // world up
}

and I have tried for days to tinker with the values, use fixed timestamps, put the camera movement in FixedUpdate/Update, use MoveTowards, and other changes, but am still experiencing issues.

Part of my problem that that the player position changes mid lerp, which causes a stutter since the target position changes in the middle of the lerp. This causes the camera to jump/sutter due to the target position of the lerp being changed in the middle of the lerp, and shakes due to the LookAt.

I would appreciate it if anyone could suggest a way to improve the camera following code as it stands now.

daniel metlitski
  • 747
  • 3
  • 11
  • 23

1 Answers1

1

Is there any particular reason you need to use the Mathf.Lerp function?

Unity has a function, Vector3.SmoothDamp that is specifically designed for movement lerping:

void FixedUpdate() {
   // Code for your desired position
   transform.position = Vector3.SmoothDamp(transform.position, wantedPosition, ref moveVelocity, dampTime);     
}

The above will smoothly follow the player by giving the SmoothDamp method control of the velocity variable. This is assuming that you supply it with a ref to store the current velocity and the damp time.

You can also adjust the damp time to change how smooth your transition is. This function will also automatically account for player movement mid-lerp.

To clarify, quoting from the documentation, dampTime in the above is:

Approximately the time it will take to reach the target. A smaller value will reach the target faster.

Also consider using Quaternion.slerp to smoothly rotate between the two rotations.

user886
  • 1,149
  • 16
  • 16
  • Here is what I changed my code to to use your suggestion: https://hastebin.com/inupemituq.cs and here is what it looks like now https://www.youtube.com/watch?v=nA3Pp4qOKwE so the issue is when the smoothdamp value is very high it doesnt seem to have stutter but the rotation with a and d is extremely unresponsive – daniel metlitski Apr 13 '17 at 04:23
  • 1
    Try setting the damp time to a constant float value. I had mine set to 0.2f and it seemed to work well. – user886 Apr 13 '17 at 04:27
  • Setting it to a constant value seems to cause the same sort of shaking – daniel metlitski Apr 13 '17 at 04:36
  • 1
    Did you try putting it in FixedUpdate? – user886 Apr 13 '17 at 04:44
  • Yes, here is the code I used https://hastebin.com/axovofotun.cs and here is a video of the results where I try out different values for the constant damp time float value https://www.youtube.com/watch?v=CMLOR6_Oq3Y – daniel metlitski Apr 13 '17 at 05:02
  • Maybe the issue is that, since position of the target that we are damping to changes midway in between the damp, is causing the stutter? – daniel metlitski Apr 13 '17 at 05:04
  • Try leaving your myTransform.LookAt (Target, -Vector3.forward); in FixedUpdate. Camera following/rotation with SmoothDamp does not need to be in the LateUpdate method. Also, try using the slerp method to go between camera rotations. https://docs.unity3d.com/ScriptReference/Quaternion.Slerp.html – user886 Apr 13 '17 at 05:08
  • Here is what it looks like when the LookAt is moved to FixedUpdate: https://www.youtube.com/watch?v=-4thYwpyNaM (code: https://hastebin.com/fejadipuqu.cs) – daniel metlitski Apr 13 '17 at 05:20