O'Reilly logo

Unity Multiplayer Games by Alan Stagner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required