Lerp: inconsistent movement(multiplayer related)

I’m currently making a multiplayer game where players can fire with cannons off a ship, while an other player controls it. The ships position is lerped on the clients from the current position to the next received position. When the players, which do not own the ship and therefore lerp the position of it, shoot with a cannon while the ship is moving, the cannonballs jitter back and fourth. I think the problem would also be noticed if an other ship(or an other object) with the same velocity moves alongside the ship. Thats propably because of dropped network packets and always changing distances Vector3.Lerp got to bypass. Does someone got a similiar problem or knows a solution to avoid this problem? It would be great if you could help me :).

At first you should not lerp to the new position but use the new position + velocity to estimate the REAL current position (reduces lag and may also reduce your jitter problem). This will give a position which you could use with Vector3.SmoothDamp to calculate a smoothed display position:


Updates for a single ship should at least contain these values:

  • ship’s ID (the way you need it)
  • ship’s current position
  • ship’s current velocity
  • current round time (= time since battle started)

The client who displays the ship should look similar like this:

// Ship's pseudo declaration:
var networkPosition = Vector3.zero;
var networkVelocity = Vector3.zero;
var networkTime     = Vector3.zero;

var velocity = Vector3.zero;

// Position Update received

var networkUpdate = ...;

var ship = getShip( networkUpdate.ID );

ship.networkPosition = networkUpdate.position;
ship.networkVelocity = networkUpdate.velocity;
ship.networkTime     = networkUpdate.time;

// every frame (Update) for each ship

var ship = ...;

var estimatedPosition = ship.networkPosition + ship.networkVelocity * ( currentRoundTime - ship.networkTime );
ship.transform.position = Vector3.SmoothDamp( ship.transform.position, estimatedPosition, ref ship.velocity, 0.2f );