For the complete documentation index, see llms.txt. This page is also available as Markdown.

Motion Prediction

Summary

Shows how to customise entity movement prediction using predictors.

  • Configure predictors using predictor assets.

  • Give an overview of the built-in predictors.

  • Configure which properties are predicted and their prediction behaviour.

  • Create a basic custom predictor that uses Vector3.SmoothDamp on position and linearly interpolates rotation.

Predictor Assets

Predictors are used to predict or smooth entity motion. Predictors receive server transform and property updates, and control the transform and property values on the client. You can change the default predictors in the 'Default Predictors' section of the ksRoomType component. There are four predictor fields:

  • Entity The default predictor to use for entities that do not use input prediction. By default this is 'DefaultLinearPredictor'.

  • Controller The default predictor to use for entities controlled by the local player. By default this is 'DefaultConvergingInputPredictor' If the player controller has ksPlayerController.UseInputPrediction set to false, the controlled entity will use the 'Entity' predictor instead.

  • Room The predictor for predicting room properties. By default this is none, which means room properties are not predicted.

  • Player The predictor for predicting player properties. By default this is none, which means player properties are not predicted.

You can assign different predictors to an entity using the following fields in the ksEntityComponent inspector:

  • Predictor The predictor to use when the entity is not controlled by the local player. If the checkbox is unchecked, it will use the default 'Entity' predictor from the ksRoomType.

  • Controller Predictor The predictor to use when the entity is controlled by the local player. If the checkbox is unchecked, it will use the default 'Controller' predictor from the ksRoomType.

Some predictors, such as the ksConvergingInputPredictor, can only be used with player controllers and can only be assigned to the ksRoomType's 'Controller' field or the entity's 'Controller Predictor'.

Which Predictor an Entity Uses

Each client chooses a predictor for each entity based only on whether that client controls the entity. What moves the entity on the server doesn't matter.

The entity, as seen by this client
Predictor
Default

Controlled by the local player with a player controller that uses input prediction (UseInputPrediction, on by default)

ksEntity.InputPredictor

The room type's 'Controller' predictor, or the entity's 'Controller Predictor'

Everything else: entities controlled by other players, entities moved only by server scripts or physics, and local entities whose controller doesn't use input prediction

ksEntity.NonInputPredictor

The room type's 'Entity' predictor, or the entity's 'Predictor'

Owned by the local player with the TRANSFORM permission

None. The client moves the entity directly.

Other clients never receive the player controller for an entity they don't control, so a player's own entity uses the input predictor on their client and the non-input predictor everywhere else. A locally controlled entity that server scripts also move still uses the input predictor; the server's changes arrive as server state the predictor converges toward.

Predictor types

ksLinearPredictor

ksLinearPredictor is the default predictor for entities without player controllers. It will linearly interpolate between each server position. If it reaches the last known server position before a new server position is received, it continues to linearly extrapolate. The linear predictor uses the ksTimeKeeper to determine which point in time to render the entity at. This ensures all entities using the linear predictor are rendered at the same point in time. The time keeper has some properties you can set to configure the behaviour of the linear predictor. Create a linear predictor asset whose properties you can edit by right-clicking in the project window and selecting 'Create->Reactor->Linear Predictor', or use the built-in 'DefaultLinearPredictor' asset.

Connect Script

Entities that have owners with the ksOwnerPermissions.TRANSFORM permission use client-authoritative movement. They use a different time keeper that uses time deltas synced from the player who owns the entity. You can access an owning player's time data and time keeper using Room.GetTime(playerId) and (ksTimeKeeper)Room.GetTime(playerId).Adjuster.

ksConvergingInputPredictor

ksConvergingInputPredictor is the default predictor for entities with player controllers that use input prediction. Accelerations are slower than on the server so that the client and server will converge. Create a converging input predictor asset whose properties you can edit by right-clicking in the project window and selecting 'Create->Reactor->Converging Input Predictor', or use the built-in 'DefaultConvergingInputPredictor' asset.

Configuring the Converging Input Predictor From Code

The asset's settings are in the predictor's Config, a ksConvergingInputPredictor.ConfigData. Two things to know before changing it at runtime:

  • The config is shared. Every entity created from the same predictor asset gets the same ConfigData object, so changing a field changes it for all of them. Clone the config, change the clone, and assign it back to give one entity its own settings.

  • Config is null while the predictor uses the built-in defaults. Clone ksConvergingInputPredictor.ConfigData.Default in that case.

ConfigData.MoveCheck is the collision check the predictor runs when it moves the entity: a sweep and slide (4 iterations and up to 90 degrees of total slide angle by default, from the asset's 'Sweep And Slide Iterations' and 'Max Total Slide Angle'), followed by depenetration. Sliding follows the contact normal, so an entity pushed against a sloped surface, such as a ball or another vehicle, can be moved upward. You can wrap the check to constrain the result.

The following client entity script keeps a ground vehicle on the plane Y = 0 during prediction.

Client Entity Script

If you assign a different predictor to Entity.InputPredictor later, configure the new one the same way.

MoveCheck only constrains the predictor's own moves. It doesn't run on the motion your player controller produces when it processes input, and server corrections bypass it. Because the player controller runs on both the client and the server, constrain movement in the controller first, and use MoveCheck to keep the predictor's smoothing within the same limits.

Rigidbody constraints, such as freezing the Y position, don't apply to predicted movement. On the client, Reactor writes the entity's transform directly, and the Unity rigidbody is kinematic unless the local player owns the entity with the TRANSFORM permission. The constraints only affect the server's physics simulation, and reach the client as server corrections.

ksClientInputPredictor

ksClientInputPredictor Predicts movement and properties using only the player controller; server data is ignored. This is mostly useful for debugging the see how your player controlled entity behaves on the client without influence from the server.

Predicting Properties

Predictors can predict property values as well as transform data. To predict properties using a linear predictor or converging input predictor, you will need to create a linear predictor asset ('Create/Reactor/Linear Predictor') or converging input predictor asset ('Create->Reactor->Converging Input Predictor'), and configure the Predicted Properties in the inspector with the properties you want to predict. Each property has a 'Type' that determines how the property will be predicted. It can be set to one of the following:

  • Linear Float - Use linear interpolation on a float.

  • Linear Vector2 - Use linear interpolation on a ksVector2.

  • Linear Vector3 - Use linear interpolation on a ksVector3.

  • Linear Color - Use linear interpolation on a ksColor.

  • Wrap Float - Use linear interpolation on a float and wrap onto a range defined by a min and max value.

  • Spherical Vector2 - Use spherical interpolation on a ksVector2.

  • Spherical Vector3 - Use spherical interpolation on a ksVector3.

  • Quaternion - Use spherical interpolation on a ksQuaternion.

  • Client - The client player controller has full control over the local property value. If the controller does not set the property value, the server value is used. The server is still authoritative; the client does not decide what other clients see.

Changing Predictors at Runtime

To change the predictor used by an entity at runtime, assign a predictor to ksEntity.InputPredictor if the entity is controlled by the local player, and ksEntity.NonInputPredictor if it is not. ksEntity.Predictor is the active preditor the entity is using. You can change the predictor used for room or player properties by assigning ksRoom.Predictor or ksPlayer.Predictor.

Set ksEntity.PredictionEnabled to enable or disable prediction for an entity at runtime. When prediction is disabled the entity will not use either the InputPredictor or the NonInputPredictor and ksEntity.Predictor will be null.

The following room script shows how to dynamically disable prediction for distant entities. For large worlds with thousands of moving entities, this can improve the performance of the client.

Client Room Script

Following an Entity With a Camera

Reactor updates entity transforms in a step of Unity's player loop that runs right after PreUpdate, before any MonoBehaviour.Update. By the time Update and LateUpdate run, entity transforms are final for the frame; FixedUpdate runs earlier and sees the previous frame's transforms.

To follow an entity with a camera, read the entity's transform in LateUpdate and move the camera with it. The predictor has already smoothed the entity's motion, so smoothing the camera's position again makes it lag behind the entity and can look like jitter. If you want a softer camera, smooth only what the predictor doesn't, such as the camera's rotation or height.

Custom Predictors

To create your own predictor script, right-click in the project browser. Select 'Create->Reactor->Predictor'. The following is a very basic sample predictor that update the entity's position using Vector3.SmoothDamp, linearly interpolates rotation, and updates a float property with id 0 using Mathf.SmoothDamp. It can be used with or without a player controller, or just to predict property 0 on a room or player. To create an asset from it that can be assigned to an entity or as a default predictor in the room type, right-click in the project browser and select 'Create->Reactor->SamplePredictor'.

Sample Predictor

Last updated