Jittery rigidbody2d movement

I have a spaceship that simply always moves forward at the same speed, and turns left or right by rotating it. What I have noticed though is that on close inspection, the spaceship doesn’t smoothly go in one direction. It jitters back and forward slightly every second or two.

private void Start() {
   SetVelocity();
}

private void Update() {
   HandleTurning();
}

private void HandleTurning() {
   if (Input.GetKey(KeyCode.A)) {
      TurnLeft();
      SetVelocity();
   } else if (Input.GetKey(KeyCode.D)) {
      TurnRight();
      SetVelocity();
   }
}

private void SetVelocity() {
   rigidBody.velocity = transform.up * 3.5f;
}

private void TurnLeft() {
   transform.Rotate(Vector3.forward * 100f * Time.deltaTime);
}

private void TurnRight() {
   transform.Rotate(Vector3.forward * 100f * Time.deltaTime * -1);
}

I’ve tried various things, like change Update to LateUpdate, and also changed the rigidbody to interpolate & exterpolate, and the jittering is still there. Any tips on how I can improve my logic or even a completely new way to handle movement?

Bump

Anyone got any ideas?

Hmm, are you sure that this script is causing issues? Here’s what comes in mind:

  1. Your camera is tracking the object at the wrong time. Try moving the logic on the camera script into LateUpdate(). It’s quite a common issue so that’s why I mentioned it.
  2. You seem to set your velocity only on start and during input when the player is turning. If your Rigidbody has some drag set up, your ship might decelerate over time. Once you press a button it will instantly change velocity.

Number 1 is most likely the issue here. Try it and come back here.

My camera doesn’t move. But I’m wondering if using transform.Rotate is an issue? Reading through the rigidbody2d docs, it mentions I shouldn’t be using transform to move a rigidbody object. I tried using MoveRotation in fixedupdate, but then the object won’t move in the rotated direction anymore.

so I dont know how relevant this is still, but I was having a similar issue. try moving your HandlingTurning into a void FixedUpdate instead of just Update.