Jittering while camera is moving

I have a weird problem that happens only in build and not in editor. In short I took the vehicle example, I removed the part of code that moved camera and wrote my own camera follow script:

public class CameraFollow : Script
{
    public Actor Target;

    public float CameraDistance = 500.0f;

    public override void OnFixedUpdate()
    {
        var transform = Actor.Transform;
   
        var targetPos = Target.Position + Vector3.Up * 100;
        var direction = (targetPos - Actor.Position).Normalized;

        transform.Translation = targetPos - CameraDistance * direction;
        transform.Orientation = Quaternion.Invert(Quaternion.LookAt(transform.Translation, Target.Position));

        Actor.Transform = transform;
    }
}

In editor everything seems absolutely fine but when I build and run my game, car that is followed by camera jitters while moving. Also I don’t think it is a physics problem because when camera is stationary another car moves smoothly same as in editor.
Recorded problem here.

Also sometimes when I build it works fine but that happened like three times and when I build again without changing anything it doesn’t work.

I have no idea why this is happening, help would be appreciated.

A tip is the camera code should not be in fixed update, but rather just update. You can also use the Lerp function for transform to assist in smoothing your camera movement out. I have seen people also have better results when they change their Time settings around the target fps.

1 Like

Lerp seems to fix this. Thank you for your help.

I’m a little late, but 343Industries did explained theoretically persuasive solution at GDC:

Basically, both Update() and FixedUpdate() is needed.

  1. You update your latest user interactions in Update() which has high frequency
  2. Do a calculation of ‘Keyframe location’ in FixedUpdate() which has the latest updated physics informations.
  3. At the latter part of Update(), you update your mobile object’s location by ‘tweening’ or ‘lerping’ stored keyframe locations with your dynamic deltaTime of framerate.

I implemented this in FlaxEngine, but FixedUpdate() and Update() has weird independant desynced timeflow, which made me do a workaround solution instead of measuring the time difference since the last FixedUpdate() in the Update().

Also, we did some improvements to engine loop that should stabilize jittering due to better sync of Draw/Update/FixedUpdate events. It will be available in the upcoming Flax 1.8.1 patch update,.

1 Like

I should check!