Prediction error smoothing
Prediction errors can occur for a number of reasons: variations in logic between clients and the server, packet drops, quantization errors, and so on. For predicted entities the net effect is that when rolling back and resimulating from the latest available snapshot, there can be a significant difference between the recomputed values and the originally predicted values. The GhostPredictionSmoothingSystem system provides a way of reconciling and reducing these errors over time to make the transitions between the two states smoother.
For each component, you can configure how to manage these errors by registering a Smoothing Action Function on the GhostPredictionSmoothing singleton which smooths the error out over time.
Note
Prediction error smoothing corrects mispredictions. It doesn't smooth the frames between two full ticks, that's full tick smoothing. Refer to Smoothing for an overview of the three smoothing features.
var ghostPredictionSmoothing = SystemAPI.GetSingleton<GhostPredictionSmoothing>();
//pass null as user data
ghostPredictionSmoothing.RegisterSmoothingAction<LocalTransform>(state.EntityManager, CustomSmoothing.Action);
//will pass as user data a pointer to a MySmoothingActionParams chunk component
ghostPredictionSmoothing.RegisterSmoothingAction<LocalTransform, CustomSmoothingActionParams>(state.EntityManager, CustomSmoothing.Action);
The user data must be a chunk-component present in the entity. A default implementation for smoothing out any translation prediction errors is provided by the package.
[BurstCompile]
public unsafe class CustomSmoothing
{
public static readonly PortableFunctionPointer<GhostPredictionSmoothing.SmoothingActionDelegate>
Action =
new PortableFunctionPointer<GhostPredictionSmoothing.SmoothingActionDelegate>(SmoothingAction);
[BurstCompile(DisableDirectCall = true)]
private static void SmoothingAction(IntPtr currentData, IntPtr previousData, IntPtr userData)
{
ref var trans = ref UnsafeUtility.AsRef<LocalTransform>(currentData.ToPointer());
ref var backup = ref UnsafeUtility.AsRef<LocalTransform>(previousData.ToPointer());
var dist = math.distance(trans.Position, backup.Position);
//UnityEngine.Debug.Log($"Custom smoothing, diff {trans.Value - backup.Value}, dist {dist}");
if (dist > 0)
trans.Position = backup.Position + (trans.Position - backup.Position) / dist;
}
}
Smoothing frequency
The PredictionSmoothingSystem doesn't correct prediction errors every single frame (at the rendering frame rate). The registered corrective actions are invoked only when a client reconciles its state because a new snapshot (containing predicted ghosts) is received from the server.
During the resimulation, when the current server tick equals the last predicted full tick, the smoothing corrections are applied to all ghosts.
Note
The only requirement for the smoothing action to run is that the client rewinds and resimulates its state (which requires predicted ghost state updates). The correction applied to the individual entities is not correlated to the fact that a state update has been received for them in the last packet.
Snap large position changes
A smoothing action receives only the corrected value and the previous one, so it can't tell a large prediction error from a position that your own code changed on purpose, such as a teleport. Unity Netcode has no dedicated teleport API. A teleport is gameplay movement like any other, so perform it in the prediction loop the same way as the rest of the ghost's movement.
Beyond a certain distance, smoothing gives up and the ghost snaps to the new position. The packaged action for position, DefaultTranslationSmoothingAction, snaps when the position changes by more than maxDist, which defaults to 10 units. To make teleports snap rather than glide, set maxDist lower than your shortest teleport distance. Add a DefaultSmoothingActionUserParams component to a predicted ghost on the client to set the threshold for that ghost:
// Snap instead of smoothing whenever the position changes by 2 units or more,
// so that short teleports aren't mistaken for a prediction error.
entityManager.AddComponentData(predictedGhost, new DefaultSmoothingActionUserParams
{
maxDist = 2f,
delta = 1f,
});
If you don't register a smoothing action, a teleport always snaps. For interpolated ghosts, use GhostFieldAttribute.MaxSmoothingDistance instead.
Limitations and known issues
- The quality of the correction depends on the frequency with which predicted ghost data is received.
- Jittery connections and lag spikes can affect the prediction correction.
- Large numbers of replicated ghosts (either predicted or replicated), or in general predicted ghost updates being received for a certain amount of time, also affect the frequency of the correction.
- Structural changes (such as adding or removing a component on a predicted ghost entity) may prevent corrections from being applied. Entities that have changed chunks or that are still in the same chunk but have been moved since the last prediction history backup will not have their prediction error smoothing applied.
- Because prediction error smoothing is based on function pointer callbacks, there may not be enough context or flexibility inside the smoothing function implementation to apply the logic you may need. The
Smoothing Actiondelegates are supposed to be simple and stateless.