Implementing server authoritative movement

Now that we have our script structured the way we need, we can go ahead and implement the rest. First, we'll create a structure to hold move commands:

// represents a move command sent to the server
private struct move
{
  public float HorizontalAxis;
  public float VerticalAxis;
  public double Timestamp;

  public move( float horiz, float vert, double timestamp )
  {
    this.HorizontalAxis = horiz;
    this.VerticalAxis = vert;
    this.Timestamp = timestamp;
  }
}

On the server, we'll store a buffer of move states sent from the client. We'll also keep a reference to our Player script for the client and server:

// a history of move states sent from client to server List<move> moveHistory = new List<move>(); // a reference to ...

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.