Camera follows car smoothly, but background is jerky?

Hi there -

I am noticing that my camera (per the 2D Unity tutorial) follows the little delivery driver car just fine - but the background seems to jerk a bit whenever I first press left or right. If I hold left or right down, it’s smooth - likewise if I don’t press it at all. But the moment I press - the background seems to jump.

I’ve tried changing the car’s Input checks to FixedUpdate, and the camera itself is being updated on LateUpdate, but I’m not sure what’s going on?

Here’s the code I’m working with:

Camera Follow:

void LateUpdate()
    {
        transform.position = followTarget.transform.position + new Vector3(0,0,-zDepth);
    }

And in the driver code:

 float steerAmt = Input.GetAxis("Horizontal") * -steerSpeed * Time.deltaTime;
float moveAmt = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
        
transform.Translate(0,moveAmt,0);
transform.Rotate(0,0,steerAmt);

Hi,

The problem is very likely caused by the physics simulation. If we keep moving the car into a collider, the physics simulation will try to make the car leave the collider.

Have you already tried to set the Collision Detection of the player’s Rigidbody2D component to “Continuous”? Also try to make the car slower to see if the behaviour improves.

Wow you’re fast! :slight_smile:

I know what you mean about moving the car into a collider - but I actually turned off all the box colliders on everything in the scene except the car itself.

Setting to continuous didn’t change it. Slowing down the car did help a little, but at that point it was moving so slowly it didn’t match anything close to the tutorial (and was also miserable to drive :smiley: )

PROGRESS: I changed the camera code to this:

transform.position = Vector3.MoveTowards(transform.position,followTarget.transform.position + new Vector3(0,0,-zDepth),0.1f);

And it revealed the car is what’s jittering. So it’s something about either the transform.translate or the Input capture.

As another test, I created an empty 2d capsule and added the same driver script - they both jitter at the same rate and at the same time. So I don’t believe it’s anything to do with the 2D collider or the rigidbody. Going to try checking the input system now…

NEW UPDATE:
I switched the system to drive automatically, using this:

steerAmt = 180 * Time.deltaTime;
moveAmt = 20 * Time.deltaTime;

transform.Rotate(0,0,-steerAmt);
transform.Translate(0,moveAmt,0);

And it still jerks. Really struggling to figure out what I’m missing here.

NEW UPDATE:

So I completely turned off the regular car (with all the box colliders, etc) and only ran the empty Capsule sprite - and it moved smoothly, except when I first turn. (The game is now jittering constantly, and I do not know why)

I am truly stumped at this point.

Final Update:

Realized I was using a different Unity version - switched to that one, and the code works. NO IDEA WHY. NOT ASKING QUESTIONS. PRAYING THE ELDRITCH GODS FORGET MY FACE.

Thanks for the help!

I’m glad using another version of Unity fixed the issue for you. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms