Class NetworkTransform
A component for syncing transforms. NetworkTransform will read the underlying transform and replicate it to clients. The replicated value will be automatically be interpolated (if active) and applied to the underlying GameObject's transform.
Inherited Members
Namespace: Unity.Netcode.Components
Assembly: Unity.Netcode.Components.dll
Syntax
[DisallowMultipleComponent]
[AddComponentMenu("Netcode/Network Transform")]
[DefaultExecutionOrder(100000)]
public class NetworkTransform : NetworkBehaviour
Fields
InLocalSpace
Sets whether the transform should be treated as local (true) or world (false) space.
Declaration
[Tooltip("Sets whether this transform should sync in local space or in world space")]
public bool InLocalSpace
Field Value
Type | Description |
---|---|
bool |
Remarks
This is synchronized by authority. During runtime, this should only be changed by the authoritative side. Non-authoritative instances will be overridden by the next authoritative state update.
Interpolate
When enabled (default) interpolation is applied. When disabled interpolation is disabled.
Declaration
public bool Interpolate
Field Value
Type | Description |
---|---|
bool |
Remarks
This is synchronized by authority and changes to interpolation during runtime forces a teleport/full update. During runtime, this should only be changed by the authoritative side. Non-authoritative instances will be overridden by the next authoritative state update.
OnClientRequestChange
The handler that gets invoked when server receives a change from a client. This handler would be useful for server to modify pos/rot/scale before applying client's request.
Declaration
public NetworkTransform.OnClientRequestChangeDelegate OnClientRequestChange
Field Value
Type | Description |
---|---|
Network |
PositionThreshold
The position threshold value that triggers a delta state update by the authoritative instance.
Declaration
public float PositionThreshold
Field Value
Type | Description |
---|---|
float |
Remarks
Note: setting this to zero will update position every network tick whether it changed or not.
PositionThresholdDefault
The default position change threshold value. Any changes above this threshold will be replicated.
Declaration
public const float PositionThresholdDefault = 0.001
Field Value
Type | Description |
---|---|
float |
RotAngleThreshold
The rotation threshold value that triggers a delta state update by the authoritative instance.
Declaration
[Range(1E-05, 360)]
public float RotAngleThreshold
Field Value
Type | Description |
---|---|
float |
Remarks
Minimum Value: 0.00001 Maximum Value: 360.0
RotAngleThresholdDefault
The default rotation angle change threshold value. Any changes above this threshold will be replicated.
Declaration
public const float RotAngleThresholdDefault = 0.01
Field Value
Type | Description |
---|---|
float |
ScaleThreshold
The scale threshold value that triggers a delta state update by the authoritative instance.
Declaration
public float ScaleThreshold
Field Value
Type | Description |
---|---|
float |
Remarks
Note: setting this to zero will update position every network tick whether it changed or not.
ScaleThresholdDefault
The default scale change threshold value. Any changes above this threshold will be replicated.
Declaration
public const float ScaleThresholdDefault = 0.01
Field Value
Type | Description |
---|---|
float |
SlerpPosition
When true and interpolation is enabled, this will Slerp to the target position.
Declaration
[Tooltip("When enabled the position interpolator will Slerp towards its current target position.")]
public bool SlerpPosition
Field Value
Type | Description |
---|---|
bool |
Remarks
This is synchronized by authority and only applies to position interpolation. During runtime, this should only be changed by the authoritative side. Non-authoritative instances will be overridden by the next authoritative state update.
SyncPositionX
When enabled (default), the x component of position will be synchronized by authority.
Declaration
public bool SyncPositionX
Field Value
Type | Description |
---|---|
bool |
Remarks
Changes to this on non-authoritative instances has no effect.
SyncPositionY
When enabled (default), the y component of position will be synchronized by authority.
Declaration
public bool SyncPositionY
Field Value
Type | Description |
---|---|
bool |
Remarks
Changes to this on non-authoritative instances has no effect.
SyncPositionZ
When enabled (default), the z component of position will be synchronized by authority.
Declaration
public bool SyncPositionZ
Field Value
Type | Description |
---|---|
bool |
Remarks
Changes to this on non-authoritative instances has no effect.
SyncRotAngleX
When enabled (default), the x component of rotation will be synchronized by authority.
Declaration
public bool SyncRotAngleX
Field Value
Type | Description |
---|---|
bool |
Remarks
When Use
SyncRotAngleY
When enabled (default), the y component of rotation will be synchronized by authority.
Declaration
public bool SyncRotAngleY
Field Value
Type | Description |
---|---|
bool |
Remarks
When Use
SyncRotAngleZ
When enabled (default), the z component of rotation will be synchronized by authority.
Declaration
public bool SyncRotAngleZ
Field Value
Type | Description |
---|---|
bool |
Remarks
When Use
SyncScaleX
When enabled (default), the x component of scale will be synchronized by authority.
Declaration
public bool SyncScaleX
Field Value
Type | Description |
---|---|
bool |
Remarks
Changes to this on non-authoritative instances has no effect.
SyncScaleY
When enabled (default), the y component of scale will be synchronized by authority.
Declaration
public bool SyncScaleY
Field Value
Type | Description |
---|---|
bool |
Remarks
Changes to this on non-authoritative instances has no effect.
SyncScaleZ
When enabled (default), the z component of scale will be synchronized by authority.
Declaration
public bool SyncScaleZ
Field Value
Type | Description |
---|---|
bool |
Remarks
Changes to this on non-authoritative instances has no effect.
UseHalfFloatPrecision
Enable this to use half float precision for position, rotation, and scale. When enabled, delta position synchronization is used.
Declaration
[Tooltip("When enabled, this will use half float precision values for position (uses delta position updating), rotation (except when Quaternion compression is enabled), and scale.")]
public bool UseHalfFloatPrecision
Field Value
Type | Description |
---|---|
bool |
Remarks
This is synchronized by authority. During runtime, this should only be changed by the authoritative side. Non-authoritative instances will be overridden by the next authoritative state update.
UseQuaternionCompression
Enabled this on the authority side for quaternion compression
Declaration
[Tooltip("When enabled, this uses a smallest three implementation that reduces full Quaternion updates down to the size of an unsigned integer (ignores half float precision settings).")]
public bool UseQuaternionCompression
Field Value
Type | Description |
---|---|
bool |
Remarks
This has a lower precision than half float precision. Recommended only for low precision
scenarios. Use
UseQuaternionSynchronization
Enable this on the authority side for quaternion synchronization
Declaration
[Tooltip("When enabled, this will synchronize the full Quaternion (i.e. all Euler rotation axis are updated if one axis has a delta)")]
public bool UseQuaternionSynchronization
Field Value
Type | Description |
---|---|
bool |
Remarks
This is synchronized by authority. During runtime, this should only be changed by the authoritative side. Non-authoritative instances will be overridden by the next authoritative state update.
UseUnreliableDeltas
Enabled by default. When set (enabled by default), NetworkTransform will send common state updates using unreliable network delivery to provide a higher tolerance to poor network conditions (especially packet loss). When disabled, all state updates are sent using a reliable fragmented sequenced network delivery.
Declaration
[Tooltip("When set, NetworkTransform will send common state updates using unreliable network delivery to provide a higher tolerance to poor network conditions (especially packet loss). When disabled, all state updates are sent using reliable fragmented sequenced network delivery.")]
public bool UseUnreliableDeltas
Field Value
Type | Description |
---|---|
bool |
Remarks
The following more critical state updates are still sent as reliable fragmented sequenced:
- The initial synchronization state update
- The teleporting state update.
- When using half float precision and the
NetworkDeltaPosition
delta exceeds the maximum delta forcing the axis in question to be collapsed into the core base position, this state update will be sent as reliable fragmented sequenced.
In order to preserve a continual consistency of axial values when unreliable delta messaging is enabled (due to the possibility of dropping packets), NetworkTransform instances will send 1 axial frame synchronization update per second (only for the axis marked to synchronize are sent as reliable fragmented sequenced) as long as a delta state update had been previously sent. When a NetworkObject is at rest, axial frame synchronization updates are not sent.
m_AddLogEntry
Declaration
protected NetworkTransform.AddLogEntryHandler m_AddLogEntry
Field Value
Type | Description |
---|---|
Network |
m_CachedIsServer
Internally used by Network
Declaration
protected bool m_CachedIsServer
Field Value
Type | Description |
---|---|
bool |
m_CachedNetworkManager
Internally used by Network
Declaration
protected NetworkManager m_CachedNetworkManager
Field Value
Type | Description |
---|---|
Network |
Properties
CanCommitToTransform
Used to determine who can write to this transform. Server only for this transform. Changing this value alone in a child implementation will not allow you to create a NetworkTransform which can be written to by clients. See the ClientNetworkTransform Sample in the package samples for how to implement a NetworkTransform with client write support. If using different values, please use RPCs to write to the server. Netcode doesn't support client side network variable writing
Declaration
public bool CanCommitToTransform { get; protected set; }
Property Value
Type | Description |
---|---|
bool |
Methods
ApplyAuthoritativeState()
Applies the authoritative state to the transform
Declaration
protected void ApplyAuthoritativeState()
Awake()
Create interpolators when first instantiated to avoid memory allocations if the associated NetworkObject persists (i.e. despawned but not destroyed or pools)
Declaration
protected virtual void Awake()
GetHalfPositionState()
Declaration
protected NetworkDeltaPosition GetHalfPositionState()
Returns
Type | Description |
---|---|
Network |
GetScale(bool)
Helper method that returns the scale of the transform.
Declaration
public Vector3 GetScale(bool getCurrentState = false)
Parameters
Type | Name | Description |
---|---|---|
bool | getCurrentState | Authority always returns the space relative transform scale (whether true or false). Non-authority: When false (default): returns the space relative transform scale When true: returns the authority scale from the most recent state update. |
Returns
Remarks
When invoked on the non-authority side: If getCurrentState is true then it will return the most current authority scale from the most recent state update. This can be useful if interpolation is enabled and you need to determine the final target scale. When invoked on the authority side: It will always return the space relative scale.
GetSpaceRelativePosition(bool)
Helper method that returns the space relative position of the transform.
Declaration
public Vector3 GetSpaceRelativePosition(bool getCurrentState = false)
Parameters
Type | Name | Description |
---|---|---|
bool | getCurrentState | Authority always returns the space relative transform position (whether true or false). Non-authority: When false (default): returns the space relative transform position When true: returns the authority position from the most recent state update. |
Returns
Remarks
If InLocalSpace is true then it returns the transform.localPosition If InLocalSpace is false then it returns the transform.position When invoked on the non-authority side: If getCurrentState is true then it will return the most current authority position from the most recent state update. This can be useful if interpolation is enabled and you need to determine the final target position. When invoked on the authority side: It will always return the space relative position.
GetSpaceRelativeRotation(bool)
Helper method that returns the space relative rotation of the transform.
Declaration
public Quaternion GetSpaceRelativeRotation(bool getCurrentState = false)
Parameters
Type | Name | Description |
---|---|---|
bool | getCurrentState | Authority always returns the space relative transform rotation (whether true or false). Non-authority: When false (default): returns the space relative transform rotation When true: returns the authority rotation from the most recent state update. |
Returns
Type | Description |
---|---|
Quaternion |
Remarks
If InLocalSpace is true then it returns the transform.localRotation If InLocalSpace is false then it returns the transform.rotation When invoked on the non-authority side: If getCurrentState is true then it will return the most current authority rotation from the most recent state update. This can be useful if interpolation is enabled and you need to determine the final target rotation. When invoked on the authority side: It will always return the space relative rotation.
GetStateId(ref NetworkTransformState)
Declaration
protected int GetStateId(ref NetworkTransform.NetworkTransformState state)
Parameters
Type | Name | Description |
---|---|---|
Network |
state |
Returns
Type | Description |
---|---|
int |
Initialize()
Initializes NetworkTransform when spawned and ownership changes.
Declaration
protected void Initialize()
IsServerAuthoritative()
Method to determine if this Network
Declaration
public bool IsServerAuthoritative()
Returns
Type | Description |
---|---|
bool | true or false |
Remarks
Used by Network
OnAuthorityPushTransformState(ref NetworkTransformState)
Invoked just prior to being pushed to non-authority instances.
Declaration
protected virtual void OnAuthorityPushTransformState(ref NetworkTransform.NetworkTransformState networkTransformState)
Parameters
Type | Name | Description |
---|---|---|
Network |
networkTransformState | the state being pushed |
Remarks
This is useful to know the exact position, rotation, or scale values sent to non-authoritative instances. This is only invoked on the authoritative instance.
OnBeforeUpdateTransformState()
Declaration
protected virtual void OnBeforeUpdateTransformState()
OnDestroy()
Invoked when the Game
Declaration
public override void OnDestroy()
Overrides
OnGainedOwnership()
Invoked on both the server and the local client of the owner when Network
Declaration
public override void OnGainedOwnership()
Overrides
OnInitialize(ref NetworkTransformState)
Invoked when first spawned and when ownership changes.
Declaration
protected virtual void OnInitialize(ref NetworkTransform.NetworkTransformState replicatedState)
Parameters
Type | Name | Description |
---|---|---|
Network |
replicatedState | the current Network |
OnInitialize(ref NetworkVariable<NetworkTransformState>)
This method is only invoked by the owner Use: OnInitialize(ref NetworkTransformState replicatedState) to be notified on all instances
Declaration
protected virtual void OnInitialize(ref NetworkVariable<NetworkTransform.NetworkTransformState> replicatedState)
Parameters
Type | Name | Description |
---|---|---|
Network |
replicatedState |
OnIsServerAuthoritative()
Override this method and return false to switch to owner authoritative mode
Declaration
protected virtual bool OnIsServerAuthoritative()
Returns
Type | Description |
---|---|
bool | (true or false) where when false it runs as owner-client authoritative |
OnLostOwnership()
Invoked on the local client when it loses ownership of the associated Network
Declaration
public override void OnLostOwnership()
Overrides
OnNetworkDespawn()
Gets called when the Network
Declaration
public override void OnNetworkDespawn()
Overrides
OnNetworkObjectParentChanged(NetworkObject)
Gets called when the parent NetworkObject of this NetworkBehaviour's NetworkObject has changed
Declaration
public override void OnNetworkObjectParentChanged(NetworkObject parentNetworkObject)
Parameters
Type | Name | Description |
---|---|---|
Network |
parentNetworkObject | the new Network |
Overrides
Remarks
When a parent changes, non-authoritative instances should:
- Apply the resultant position, rotation, and scale from the parenting action.
- Clear interpolators (even if not enabled on this frame)
- Reset the interpolators to the position, rotation, and scale resultant values. This prevents interpolation visual anomalies and issues during initial synchronization
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 virtual void OnNetworkTransformStateUpdated(ref NetworkTransform.NetworkTransformState oldState, ref NetworkTransform.NetworkTransformState newState)
Parameters
Type | Name | Description |
---|---|---|
Network |
oldState | the previous Network |
Network |
newState |
OnOwnershipChanged(ulong, ulong)
Invoked on all clients, override this method to be notified of any ownership changes (even if the instance was niether the previous or newly assigned current owner).
Declaration
protected override void OnOwnershipChanged(ulong previous, ulong current)
Parameters
Overrides
OnSynchronize<T>(ref BufferSerializer<T>)
This is invoked when a new client joins (server and client sides) Server Side: Serializes as if we were teleporting (everything is sent via NetworkTransformState) Client Side: Adds the interpolated state which applies the NetworkTransformState as well
Declaration
protected override void OnSynchronize<T>(ref BufferSerializer<T> serializer) where T : IReaderWriter
Parameters
Type | Name | Description |
---|---|---|
Buffer |
serializer |
Type Parameters
Name | Description |
---|---|
T |
Overrides
Remarks
If a derived class overrides this, then make sure to invoke this base method!
OnTransformUpdated()
Declaration
protected virtual void OnTransformUpdated()
SetMaxInterpolationBound(float)
Will set the maximum interpolation boundary for the interpolators of this Network
Declaration
public void SetMaxInterpolationBound(float maxInterpolationBound)
Parameters
Type | Name | Description |
---|---|---|
float | maxInterpolationBound | Maximum time boundary that can be used in a frame when interpolating between two values |
SetState(Vector3?, Quaternion?, Vector3?, bool)
Directly sets a state on the authoritative transform. Owner clients can directly set the state on a server authoritative transform This will override any changes made previously to the transform This isn't resistant to network jitter. Server side changes due to this method won't be interpolated. The parameters are broken up into pos / rot / scale on purpose so that the caller can perturb just the desired one(s)
Declaration
public void SetState(Vector3? posIn = null, Quaternion? rotIn = null, Vector3? scaleIn = null, bool teleportDisabled = true)
Parameters
Type | Name | Description |
---|---|---|
Vector3? | posIn | |
Quaternion? | rotIn | |
Vector3? | scaleIn | new scale to scale to. Can be null |
bool | teleportDisabled | When true (the default) the Network |
Exceptions
Type | Condition |
---|---|
Exception |
Teleport(Vector3, Quaternion, Vector3)
Teleport the transform to the given values without interpolating
Declaration
public void Teleport(Vector3 newPosition, Quaternion newRotation, Vector3 newScale)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | newPosition | |
Quaternion | newRotation | |
Vector3 | newScale | new scale to scale to. |
Exceptions
Type | Condition |
---|---|
Exception |
TryCommitTransformToServer(Transform, double)
This will try to send/commit the current transform delta states (if any)
Declaration
protected void TryCommitTransformToServer(Transform transformToCommit, double dirtyTime)
Parameters
Type | Name | Description |
---|---|---|
Transform | transformToCommit | the transform to be committed |
double | dirtyTime | time it was marked dirty |
Remarks
Only client owners or the server should invoke this method
Update()
Declaration
protected virtual void Update()
Remarks
If you override this method, be sure that:
- Non-authority always invokes this base class method.