Preparing for server authoritative movement
Before we get started, we'll need to restructure our scripts. We're going to change the Update
function so that it isn't called automatically, and instead will be called by our networking scripts. We need to do this so that we have direct control over the exact order in which the simulation is stepped, otherwise we can easily end up with desyncs and rubber banding.
using UnityEngine; using System.Collections; public class Player : MonoBehaviour { public float MoveSpeed = 5f; [System.NonSerialized] public float horizAxis = 0f; [System.NonSerialized] public float vertAxis = 0f; void Update() { if( networkView.isMine ) { horizAxis = Input.GetAxis( "Horizontal" ); vertAxis = Input.GetAxis( "Vertical" ); } } public void Simulate() { transform.Translate( new Vector3( horizAxis, 0, vertAxis ) * MoveSpeed * Time.fixedDeltaTime ); } }
A few things to take note of: instead of using Update
, we've moved our...