Dealing with jittery movement of paralax layers

Hi all,
I am using Cinemachine to follow my spaceship in top-down.
I also added two background layers that follow the camera with different speeds to create a parallax effect
layer.transform.position=Camera.main.position * factor

That led to jittery movement and I figured the reason to be as follows:

  • my background layers uses Update to set their positions
  • in LateUpdate Cinemachines calculates a new position for the main camera and sets it
  • so for that frame the background is not consistent with the actual camera position

I don’t know why this is jittery and not just one frame lagging but I guess the dampening of Cinemachines does not create equal motion every frame.

So my solution was to update the background layers also in LateUpdate and move their script in the script execution order behind Cinemachine so I know at that point my main camera has the correct position for that frame.
Somehow this feels wrong. Changing script order feels more like a last resort (maybe it is in that case). I would somehow need a LateLateUpdate :smile:
Is there any other way to deal with that problem?

Thanks!

The first thing is that you need to make sure that any code that depends on camera position - such as parallax placement - happens after the camera is positioned. With Cinemachine, this happens very late in the frame, during CinemachineBrain.LateUpdate, and CInemachineBrain has a late script update order.

One way to get around messing with script update order is to add a handler for CinemachineCore.CameraUpdatedEvent and call your parallax code from there. That event is fired every frame just after the camera is positioned.

Ohh great, thank you that works nicely.:slight_smile: I will subscribe every script in their respective Start() to that event. It is a bit coupled to Cinemachine that way but it is only 2-3 scripts I guess, so switching to another cam system - if needed - is ok.