Entity rewinding
In order to solve this, we need to store the state of entities at every network update. When the entity needs to be rewound, we find the two states on either end of the target past time, and interpolate between them. This technique actually shares many similarities with the entity interpolation code we wrote in the last chapter, and in some places you could potentially reuse some of the same code.
So, in our network player class, we'll create a new struct to store the network state:
private struct networkState { public Vector3 Position; public Vector3 Rotation; public Vector3 CamRotation; public double Timestamp; }
We'll keep a buffer of these states, using an array just as in the last chapter:
private networkState[] stateBuffer = ...
Get Unity Multiplayer Games now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.