Lerp function causing camera judder

I’m just starting to learn unity, and as a starter project I’m making a 2D golf game.

I want the camera to follow the ball, but depending on whether the ball is moving left / right, I want the camera to ‘offset’ a bit so we can see the course ahead.

Here’s an image to hopefully help explain, default follow I’ve written has ball in view at position A, I want it to be at pos B (when moving right):

Initially I had this code to set the offset:

if (followTarget.GetComponent<Rigidbody2D>().velocity.x > 0.1f)
offsetX = -(maxOffsetX);

else if (followTarget.GetComponent<Rigidbody2D>().velocity.x < -0.1f)
offsetX = maxOffsetX;

        targetPos = new Vector3(followTarget.transform.position.x - offsetX, followTarget.transform.position.y - offsetY, holeDetails.defaultCameraDistance);
        transform.position = Vector3.Lerp(transform.position, targetPos, followSpeed * Time.deltaTime);

Which worked just fine. The only issue was, when hitting the ball left AFTER hitting it right, the camera would move super quick to the opposite ‘offset’ and it looked bad.

So, I tried to write something that’d LERP the offset values. My code is below - it almost works, however it never quite reaches the maxOffsetX value and just judders really painfully. Any ideas, please? I’m sure it’s something simple, I’ve just looked at it for so long now my brain’s not working any more!! :smile:

    public void FollowBall()
    {
        float offsetAdjust;

         if (followTarget.GetComponent<Rigidbody2D>().velocity.x > 0.1f && offsetX > -(maxOffsetX))
        {

            offsetAdjust = Mathf.Lerp(0f, 2 * maxOffsetX, followSpeed * Time.deltaTime);
            offsetAdjust -= maxOffsetX;

            offsetX = offsetAdjust;

        }
        else if (followTarget.GetComponent<Rigidbody2D>().velocity.x < -0.1f && offsetX < maxOffsetX)
        {
         
                offsetAdjust = Mathf.Lerp(0f, 2 * maxOffsetX, followSpeed * Time.deltaTime);
                offsetAdjust -= maxOffsetX;

                offsetX = -offsetAdjust;
           
        }

        targetPos = new Vector3(followTarget.transform.position.x - offsetX, followTarget.transform.position.y - offsetY, holeDetails.defaultCameraDistance);
        transform.position = Vector3.Lerp(transform.position, targetPos, followSpeed * Time.deltaTime);

If you Lerp(current, target, speed * Time.deltaTime);, you’ll never reach target, unless deltaTime is over 1. There’s a good classic article on lerping here.

Your code shouldn’t cause jitter, unless followSpeed is very high and targetPos is changing erratically. Do you have a video?

If you just want to get a good camera, Cinemachine is the way to go. If you want to do it manually, the best suggestion is more springs. If you both Lerp the targetPos from frame to frame and the transform.position, you’ll give the camera some interia, which will probably feel better.

Also you could simplify your code a lot by multiplying with the sign of the velocity rather than having an if/else branch.

1 Like

Ooooh I didn’t even know that was a thing. Thanks! :smile:

Will try grab a video now.

Oh good grief, reloading unity and running it again, it now works fine (super smooth!) without any code change. Lol.

Is restarting a thing I should always try when I hit weird problems? hah.

I realise there are cool tools to use but I’m enjoying getting back into coding by manually making things at the moment!!

Thanks for your help anyway :slight_smile:

Not in general, no, but there’s been some talk in the forums that people are seeing a degradation of Unity’s performance as they work, which fixes itself after a restart. So it might be that that was the problem - ie. your camera wasn’t laggy, everything was.

I haven’t been experiencing that myself, so I can’t guarantee it, but that’s my best guess for “restart fixed the problem”.

1 Like

Back in Unity3.x days I routinely had a stuttering editor. Not often, and only for a few seconds, but I learned to double-check any time I saw stuttering to be sure my code was the problem.

As far as camera stutter, the main reason for me is being cute and moving it in FixedUpdate instead of Update.