I have a Rigidbody2D vehicle with interpolation on, it’s properly moved in FixedUpdate. Camera movement is on LateUpdate with SmoothDamp on. We have huge open worlds so I’m using origin shifting. Here’s the parts of camera code that matter
void LateUpdate()
{
targetPos.Set(vehTR.position.x + camOffsetCurve.Evaluate(targetVehicle.Velocity.x), vehTR.position.y + camOffsetCurve.Evaluate(targetVehicle.Velocity.y), -10f);
ClampTargetPos();
if (CamMode == CameraModes.Active) MoveCamera();
...
}
private void MoveCamera()
{
transform.position = Vector3.SmoothDamp(transform.position, clampedPos, ref velocity, driveCamSmoothTime);
}
private void ShiftCameraToOrigin(Vector3 offset)
{
if (CamMode == CameraModes.Inactive) return;
transform.position += offset;
}
In Vehicle script:
private void ResolveOriginShift(Vector3 shiftPos)
{
RB.position += (Vector2)shiftPos;
}
It was all working flawlessly before upgrading from 2022.3.35f1 to 2022.3.49f1 but after the upgrade when origin shift happens camera is glitching. I isolated the case just to editor version upgrade so it’s %100 related to that. When I turn interpolation off of the vehicle the problem goes away but obviously whole smooth camera follow is messed up. Turning SmoothDamp off also “works” but that’s visually bad too. Then I checked the changelogs and saw this:
Physics 2D: When using Rigidbody2D interpolation and changing the Transform directly, the Transform pose has the priority for interpolation whereas when changing the Rigidbody2D pose directly, the Rigidbody2D pose has the priority over the Transform. This stops “jitter” issues when driving the Rigidbody2D constantly via the Transform. This still isn’t a recommended practice however. (UUM-82789)
I guess this change is the reason. Am I doing something wrong (even though there were no problems before the upgrade), should I wait for a fix or have to do something about this?
If you want interpolation support then you should use MovePosition. If you set the body position directly then it has to turn off interpolation until the next simulation step.
Hi Melv, thanks for the quick reply. I changed the Vehicle origin shift code to this:
private void ResolveOriginShift(Vector3 shiftPos)
{
RB.MovePosition(RB.position + (Vector2)shiftPos);
}
but it’s still the same. Am I understanding your solution correctly?
It’s called on another script in FixedUpdate
private void FixedUpdate()
{
HandleOriginShift();
}
private void HandleOriginShift()
{
if (Mathf.Abs(tr.position.x) >= shiftThreshold || Mathf.Abs(tr.position.y) >= shiftThreshold)
{
worldData.ShiftWorldBoundaries(-tr.position);
worldGrid.ShiftChunks(-tr.position);
eventRelay.Raise_On_OriginShift(-tr.position);
tr.position = Vector2.zero;
}
}
This one eventRelay.Raise_On_OriginShift(-tr.position);
To be clear here, you’re passing in “-tr.position” and you somehow calculate where that is based upon the body position right and NOT against the Transform position (which won’t be the body position when interpolating) ? During interpolation, the Transform is “behind” the real body position i.e. it’s historic.
Know that the change you referred to fixed several bugs and found a happy middle ground where devs were changing the Transform, the Rigidbody2D position/rotation and using MovePosition/MoveRotation when using interpolation and sometimes all at the same time. It’s practically impossible to “fix” all these so the change was to make a sane choice on what should happen. Change the Rigidbody2D and it uses that, change the Transform and it uses that.
In a handful of cases, this’ll expose a flaw in the movement behaviour/usage. I only say this because a change might simply reveal that.
Can you replicate this in a simple reproduction project i.e. a rigidbody and a camera? If you can then I can look at it immediately.
I don’t really want to waste your time. I’ll try to make it work again or find some kind of a workaround. If I can’t solve it I’ll send you a simple reproduction project. Thanks a lot man I appreciate it.
It’s absolutely not a waste of time, more than happy to help. Ping me if you need help. 
Based on your explanation I found a very simple workaround. Posting here just in case someone else needs it. When teleporting the vehicle during origin shift I sync transform and rigidbody2d like this:
private void ResolveOriginShift(Vector3 shiftPos)
{
RB.position += (Vector2)shiftPos;
TR.position = RB.position;
}
and it’s working perfectly. Zero stutters or glitches whatsoever. Thanks again!
1 Like
Cool, thanks for letting me know!
1 Like