Class AnticipatedNetworkTransform
A subclass of Network
- Snap: In this mode (with Stale
Data set to Ignore and no OnHandling Reanticipate(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 Stale
Data set to Ignore and an OnHandling Reanticipate(double) callback that calls Smooth(TransformState, Transform 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.State, float) - Constant Reanticipation: In this mode (with Stale
Data set to Reanticipate and an OnHandling Reanticipate(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, Transform 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.State, float)
Note that these three modes may be combined. For example, if an On
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 |
---|---|
Stale |
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 Previous
Declaration
public AnticipatedNetworkTransform.TransformState AnticipatedState { get; }
Property Value
Type | Description |
---|---|
Anticipated |
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 |
---|---|
Anticipated |
PreviousAnticipatedState
Holds the most recent anticipated state, whatever was
most recently set using the Anticipate methods. Unlike
Anticipated
Declaration
public AnticipatedNetworkTransform.TransformState PreviousAnticipatedState { get; }
Property Value
Type | Description |
---|---|
Anticipated |
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 Previous
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 |
---|---|---|
Anticipated |
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 Game
Declaration
public override void OnDestroy()
Overrides
OnNetworkDespawn()
Gets called when the Network
Declaration
public override void OnNetworkDespawn()
Overrides
OnNetworkSpawn()
Gets called when the Network
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 |
---|---|---|
Network |
oldState | the previous Network |
Network |
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
delta
Declaration
public void Smooth(AnticipatedNetworkTransform.TransformState from, AnticipatedNetworkTransform.TransformState to, float durationSeconds)
Parameters
Type | Name | Description |
---|---|---|
Anticipated |
from | |
Anticipated |
to | |
float | durationSeconds |