ClientSidePrediction icon indicating copy to clipboard operation
ClientSidePrediction copied to clipboard

Discord Releases GitHub Sponsors

ClientSidePrediction

prediction for a single object, very basic prototype

Video: https://youtu.be/L6Nih4T0iI4

How to use

PredictionManager

The PredictionManager class sets up the the client side prediction. It will set up the simulation automatically based on the physicsMode, if local physics is selected PredictionManager will need to be in the correct scene when it's Start method is called by unity.

SetPredictionSimulation can be used to set the set custom simulation. Giving a DefaultPredictionSimulation will allow you to use local physics in a scene that the PredictionManager is not in.

PredictionBehaviour

Objects must have a PredictionBehaviour on the object to be effected by client side prediction.

Inputs

For a behaviour to send inputs to the server it must override the HasInput property and return true.

Input functions:

  • GetInput called once per tick on client, should return inputs for the current tick
  • ApplyInput called client and server before the tick is simulated. Used to apply inputs to an object, eg add force to rigid based on WASD keys. For inputs like jumping or single frame actions you should compare the inputs to the previous ones to see if a button was pressed this tick.
  • MissingInput called on the server if input is missing for a tick. This can be used to continue inputs that were previously received, most of the time this will work because user inputs will be almost always the same frame to frame. previous and previousTick are the values from the last received input, if 2 inputs have been missed they will be from N-2 tick. the currentTick is the tick that the inputs are missing for.

State and simulation

  • ApplyState call on client to set the state of the object. This should be used to set objects position, rotation or other values.
  • GatherState called on server and client to get the state from the object.
  • NetworkFixedUpdate called on server and client, called once per tick and durning resimulation on client. Should be used to modify the state, For example adding a force to a rigidbody.
  • ResimulationTransition called on client to correct the resimulation. If empty the client will appear as if it is snapped to latest resimulation state. To move out this correct you can Lerp between previous and new state and call ApplyState.

Logic flow

flow chart for function calls