Class AnticipatedNetworkTransform
A subclass of NetworkTransform that supports basic client anticipation - the client can set a value on the belief that the server will update it to reflect the same value in a future update (i.e., as the result of an RPC call). This value can then be adjusted as new updates from the server come in, in three basic modes:
- Snap: In this mode (with StaleDataHandling set to Ignore and no OnReanticipate(double) callback), the moment a more up-to-date value is received from the authority, it will simply replace the anticipated value, resulting in a "snap" to the new value if it is different from the anticipated value.
- Smooth: In this mode (with StaleDataHandling set to Ignore and an OnReanticipate(double) callback that calls Smooth(TransformState, TransformState, float) from the anticipated value to the authority value with an appropriate Lerp(float, float, float)-style smooth function), when a more up-to-date value is received from the authority, it will interpolate over time from an incorrect anticipated value to the correct authoritative value.
- Constant Reanticipation: In this mode (with StaleDataHandling set to Reanticipate and an OnReanticipate(double) that calculates a new anticipated value based on the current authoritative value), when a more up-to-date value is received from the authority, user code calculates a new anticipated value, possibly calling Smooth(TransformState, TransformState, float) to interpolate between the previous anticipation and the new anticipation. This is useful for values that change frequently and need to constantly be re-evaluated, as opposed to values that change only in response to user action and simply need a one-time anticipation when the user performs that action.
Note that these three modes may be combined. For example, if an OnReanticipate(double) callback does not call either Smooth(TransformState, TransformState, float) or one of the Anticipate methods, the result will be a snap to the authoritative value, enabling for a callback that may conditionally call Smooth(TransformState, TransformState, float) when the difference between the anticipated and authoritative values is within some threshold, but fall back to snap behavior if the difference is too large.
Inheritance
Inherited Members
Namespace: Unity.Netcode.Components
Assembly: Unity.Netcode.Runtime.dll
Syntax
[DisallowMultipleComponent]
[AddComponentMenu("Netcode/Anticipated Network Transform")]
public class AnticipatedNetworkTransform : NetworkTransform
Fields
StaleDataHandling
Defines what the behavior should be if we receive a value from the server with an earlier associated
time value than the anticipation time value.
If this is Ignore, the stale data will be ignored and the authoritative
value will not replace the anticipated value until the anticipation time is reached. OnAuthoritativeValueChanged
and OnReanticipate will also not be invoked for this stale data.
If this is Reanticipate, the stale data will replace the anticipated data and
OnAuthoritativeValueChanged and OnReanticipate will be invoked.
In this case, the authoritativeTime value passed to OnReanticipate will be lower than
the anticipationTime value, and that callback can be used to calculate a new anticipated value.
Declaration
public StaleDataHandling StaleDataHandling
Field Value
Type | Description |
---|---|
StaleDataHandling |
Properties
AnticipatedState
Contains the current anticipated state, which will match the values of this object's actual MonoBehaviour.transform. When a server update arrives, this value will be overwritten by the new server value (unless stale data handling is set to "Ignore" and the update is determined to be stale). This value will be duplicated in PreviousAnticipatedState, which will NOT be overwritten in server updates.
Declaration
public AnticipatedNetworkTransform.TransformState AnticipatedState { get; }
Property Value
Type | Description |
---|---|
AnticipatedNetworkTransform.TransformState |
AuthoritativeState
Contains the current state of this transform on the server side. Note that, on the server side, this gets updated at the end of the frame, and will not immediately reflect changes to the transform.
Declaration
public AnticipatedNetworkTransform.TransformState AuthoritativeState { get; }
Property Value
Type | Description |
---|---|
AnticipatedNetworkTransform.TransformState |
PreviousAnticipatedState
Holds the most recent anticipated state, whatever was most recently set using the Anticipate methods. Unlike AnticipatedState, this does not get overwritten when a server update arrives.
Declaration
public AnticipatedNetworkTransform.TransformState PreviousAnticipatedState { get; }
Property Value
Type | Description |
---|---|
AnticipatedNetworkTransform.TransformState |
ShouldReanticipate
Indicates whether this transform currently needs reanticipation. If this is true, the anticipated value has been overwritten by the authoritative value from the server; the previous anticipated value is stored in PreviousAnticipatedState
Declaration
public bool ShouldReanticipate { get; }
Property Value
Type | Description |
---|---|
bool |
Methods
AnticipateMove(Vector3)
Anticipate that, at the end of one round trip to the server, this transform will be in the given newPosition
Declaration
public void AnticipateMove(Vector3 newPosition)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | newPosition |
AnticipateRotate(Quaternion)
Anticipate that, at the end of one round trip to the server, this transform will have the given newRotation
Declaration
public void AnticipateRotate(Quaternion newRotation)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | newRotation |
AnticipateScale(Vector3)
Anticipate that, at the end of one round trip to the server, this transform will have the given newScale
Declaration
public void AnticipateScale(Vector3 newScale)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | newScale |
AnticipateState(TransformState)
Anticipate that, at the end of one round trip to the server, the transform will have the given newState
Declaration
public void AnticipateState(AnticipatedNetworkTransform.TransformState newState)
Parameters
Type | Name | Description |
---|---|---|
AnticipatedNetworkTransform.TransformState | newState |
InternalOnNetworkPostSpawn()
(This replaces the any subsequent OnSynchronize for NetworkTransforms post client synchronization) This occurs on already connected clients when dynamically spawning a NetworkObject for non-authoritative instances.
Declaration
protected override void InternalOnNetworkPostSpawn()
Overrides
InternalOnNetworkSessionSynchronized()
(This replaces the first OnSynchronize for NetworkTransforms) This is needed to initialize when fully synchronized since non-authority instances don't apply the initial synchronization (new client synchronization) until after everything has been spawned and synchronized.
Declaration
protected override void InternalOnNetworkSessionSynchronized()
Overrides
OnBeforeUpdateTransformState()
Declaration
protected override void OnBeforeUpdateTransformState()
Overrides
OnDestroy()
Invoked when the GameObject the NetworkBehaviour is attached to is destroyed. If you override this, you must always invoke the base class version of this OnDestroy() method.
Declaration
public override void OnDestroy()
Overrides
OnNetworkDespawn()
Gets called when the NetworkObject gets despawned. This method runs both client and server side.
Declaration
public override void OnNetworkDespawn()
Overrides
OnNetworkSpawn()
Gets called when the NetworkObject gets spawned, message handlers are ready to be registered, and the network is set up.
Declaration
public override void OnNetworkSpawn()
Overrides
OnNetworkTransformStateUpdated(ref NetworkTransformState, ref NetworkTransformState)
Invoked on the non-authoritative side when the NetworkTransformState has been updated
Declaration
protected override void OnNetworkTransformStateUpdated(ref NetworkTransform.NetworkTransformState oldState, ref NetworkTransform.NetworkTransformState newState)
Parameters
Type | Name | Description |
---|---|---|
NetworkTransform.NetworkTransformState | oldState | the previous NetworkTransform.NetworkTransformState |
NetworkTransform.NetworkTransformState | newState |
Overrides
OnTransformUpdated()
Declaration
protected override void OnTransformUpdated()
Overrides
OnUpdate()
Declaration
public override void OnUpdate()
Overrides
Remarks
If you override this method, be sure that:
- Non-authority always invokes this base class method.
Smooth(TransformState, TransformState, float)
Interpolate between the transform represented by from to the transform represented by to over durationSeconds of real time. The duration uses deltaTime, so it is affected by timeScale.
Declaration
public void Smooth(AnticipatedNetworkTransform.TransformState from, AnticipatedNetworkTransform.TransformState to, float durationSeconds)
Parameters
Type | Name | Description |
---|---|---|
AnticipatedNetworkTransform.TransformState | from | |
AnticipatedNetworkTransform.TransformState | to | |
float | durationSeconds |