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 the Player script attached to the game object. Player playerScript; // get the Player component void Start() { playerScript = GetComponent<Player>(); }
Now, in FixedUpdate
we'll grab the current move state...