Struct PredictionSwitching
Client-only, allows to change how ghost are simulated at runtime by switching in between interpolated and predicted mode.
In order to use prediction-switching, ghosts must have their `Supported Ghost Modes` set to `All` (via GhostObject) and the GhostObject.DefaultGhostMode should not be OwnerPredicted.
When the ghost mode changes, the `timeline` for that specific ghost is affected. Predicted ghost are ahead of the server, (roughly one round trip time / SimulationTickRate ticks). Interpolated ghosts are instead running behind the server timeline/last-received tick, by a variable margin (this depends both on configuration (see ClientTickRate) and network condition). By default, it'll usually be 2/3 ticks behind the last received data.
Because of that, we provide a way to smooth that transition, at the moment limited only to `visuals`, by interpolating the visual transform (entities' LocalToWorld) toward the new desired value (SwitchPredictionSmoothing component).
From a GhostBehaviour point of view, the ghost will be seen as predicted/interpolated (and the component data changed) starting from the first LateUpdate after the call. The first PredictionUpdate(PredictionUpdateContext) after a ghost has been converted is going to be invoked or skipped accordingly to requested mode. When smoothing is applied, for the whole duration of the transition, the GameObject.Transform is affected as following, trying to mimic GameObject physics' Rigidbody's interpolation behaviour:
- In the first LateUpdate after the conversion, the GameObject.Transform (position, rotation and scale) will match the interpolated position.
- For all the subsequent GameObject.Update until the transition end, the position, rotation and scale match the interpolated position.
- Inside the PredictionUpdate(PredictionUpdateContext) the gameObject.transform's position, rotation and scale match the actual gameplay predicted transform values. In the background, those are stored in LocalTransform entities side.
Inherited Members
Namespace: Unity.Netcode
Assembly: Unity.NetCode.dll
Syntax
public struct PredictionSwitching
Remarks
PredictionSwitching is a simple wrapping interface, built on top of the GhostPredictionSwitchingQueues. It is used internally to schedule the mode changes. The GhostPredictionSwitchingSystem is responsible to convert the entity from one mode to the other by:
- Adding/Removing components, based on the PrefabType rule
- Adding/Removing the PredictedGhost component, that state how the ghost should be simulated
Methods
CanSwitchToInterpolated(GhostObject)
Check if the ghost can be converted to a interpolated ghost by verifying that:
- the ghost is predicted
- the ghost is not smoothing a previous mode transition.
Declaration
[ExcludeFromBurstCompatTesting("Use managed types")]
public bool CanSwitchToInterpolated(GhostObject ghost)
Parameters
| Type | Name | Description |
|---|---|---|
| GhostObject | ghost | The ghost to check. |
Returns
| Type | Description |
|---|---|
| bool | true if the predicted ghost can be converted to interpolated. |
CanSwitchToPredicted(GhostObject)
Check if the ghost can be converted to a predicted ghost by verifying that:
- the ghost is interpolated
- the ghost is not smoothing a previous mode transition.
Declaration
[ExcludeFromBurstCompatTesting("Use managed types")]
public bool CanSwitchToPredicted(GhostObject ghost)
Parameters
| Type | Name | Description |
|---|---|---|
| GhostObject | ghost | The ghost to check. |
Returns
| Type | Description |
|---|---|
| bool | true if the interpolated ghost can be converted to be predicted. |
IsSmoothingModeChange(Entity)
Denote if the ghost has been converted to either interpolated or predicted and if it is still smoothing the transition (visually).
Declaration
public bool IsSmoothingModeChange(Entity entity)
Parameters
| Type | Name | Description |
|---|---|---|
| Entity | entity | The ghost entity to check. |
Returns
| Type | Description |
|---|---|
| bool | true if the ghost is still smoothing a mode transition. |
IsSmoothingModeChange(GhostObject)
Denote if the ghost has been converted to either interpolated or predicted and if it is still smoothing the transition (visually).
Declaration
[ExcludeFromBurstCompatTesting("Use managed types")]
public bool IsSmoothingModeChange(GhostObject ghost)
Parameters
| Type | Name | Description |
|---|---|---|
| GhostObject | ghost | The ghost to check. |
Returns
| Type | Description |
|---|---|
| bool | true if the ghost is still smoothing a mode transition. |
ToInterpolated(Entity, float)
Convert an predicted ghost to an interpolated ghost. The ghost must support both interpolated and predicted mode, and it cannot be owner predicted. Owner predicted ghost are automatically changing prediction mode when the owner is changed.
Declaration
public bool ToInterpolated(Entity entity, float transitionDuration = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Entity | entity | The ghost entity to convert. |
| float | transitionDuration | Duration in seconds of the visual smoothing applied during the transition. 0 disables smoothing. |
Returns
| Type | Description |
|---|---|
| bool | Always true. This is also the case when the ghost is already interpolated. |
Remarks
If this method is called from a MonoBehaviour, and the entity is associated to a GhostObject, all the PredictionUpdate(PredictionUpdateContext) are not going to be invoked this frame. If this method is called from a System, running after the GhostPredictionSmoothingSystem, the ghost conversion will occur the next frame, thus the ghost will be still be seen as predicted for the whole world update.
From a GhostBehaviour perspective the ghost is going to be seen as: - `Predicted` in the next Update - `Interpolated` in the next LateUpdate
ToInterpolated(GhostObject, float)
Convert a predicted ghost to an interpolated ghost. The ghost must support both interpolated and predicted mode, and it cannot be owner predicted. Owner predicted ghost are automatically changing prediction mode when the owner is changed.
Declaration
[ExcludeFromBurstCompatTesting("Use managed types")]
public bool ToInterpolated(GhostObject ghost, float transitionDuration = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| GhostObject | ghost | The ghost to convert. |
| float | transitionDuration | Duration in seconds of the visual smoothing applied during the transition. 0 disables smoothing. |
Returns
| Type | Description |
|---|---|
| bool | true if the conversion was requested, false if the ghost is not allowed to switch mode. |
Remarks
If this method is called from a MonoBehaviour, and the entity is associated to a GhostObject, all the PredictionUpdate(PredictionUpdateContext) are not going to be invoked this frame. If this method is called from a System, running after the GhostPredictionSmoothingSystem, the ghost conversion will occur the next frame, thus the ghost will be still be seen as predicted for the whole world update.
From a GhostBehaviour perspective the ghost is going to be seen as: - `Predicted` in the next Update - `Interpolated` in the next LateUpdate
ToPredicted(Entity, float)
Convert an interpolated ghost to a predicted ghost. The ghost must support both interpolated and predicted mode, and it cannot be owner predicted. Owner predicted ghost are automatically changing prediction mode when the owner is changed.
Declaration
public bool ToPredicted(Entity entity, float transitionDuration = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Entity | entity | The ghost entity to convert. |
| float | transitionDuration | Duration in seconds of the visual smoothing applied during the transition. 0 disables smoothing. |
Returns
| Type | Description |
|---|---|
| bool | Always true. This is also the case when the ghost is already predicted. |
Remarks
If this method is called from a MonoBehaviour, and the entity is associated to a GhostObject, all the PredictionUpdate(PredictionUpdateContext) are going to be invoked this frame. If this method is called from a System, running after the GhostPredictionSmoothingSystem, the ghost conversion will occur the next frame, thus the ghost will be still be seen as interpolated for the whole world update.
From a GhostBehaviour perspective the ghost is going to be seen as: - `Interpolated` in the next Update - `Predicted` in the next LateUpdate
ToPredicted(GhostObject, float)
Convert an interpolated ghost to a predicted ghost. The ghost must support both interpolated and predicted mode, and it cannot be owner predicted. Owner predicted ghost are automatically changing prediction mode when the owner is changed.
Declaration
[ExcludeFromBurstCompatTesting("Use managed types")]
public bool ToPredicted(GhostObject ghost, float transitionDuration = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| GhostObject | ghost | The ghost to convert. |
| float | transitionDuration | Duration in seconds of the visual smoothing applied during the transition. 0 disables smoothing. |
Returns
| Type | Description |
|---|---|
| bool | true if the conversion was requested, false if the ghost is not allowed to switch mode. |
Remarks
If this method is called from a MonoBehaviour, and the entity is associated to a GhostObject, all the PredictionUpdate(PredictionUpdateContext) are going to be invoked this frame. If this method is called from a System, running after the GhostPredictionSmoothingSystem, the ghost conversion will occur the next frame, thus the ghost will be still be seen as interpolated for the whole world update.
From a GhostBehaviour perspective the ghost is going to be seen as: - `Interpolated` in the next Update - `Predicted` in the next LateUpdate