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
Syntax
[DisallowMultipleComponent]
[AddComponentMenu("Netcode/NetworkTransform")]
[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 |
---|---|
Boolean |
Remarks
This should only be changed by the authoritative side during runtime. Non-authoritative changes will be overridden upon the next state update.
Interpolate
When enabled (default) interpolation is applied and when disabled no interpolation is applied
Declaration
public bool Interpolate
Field Value
Type | Description |
---|---|
Boolean |
m_CachedIsServer
Internally used by NetworkTransform to keep track of whether this NetworkBehaviour derived class instance was instantiated on the server side or not.
Declaration
protected bool m_CachedIsServer
Field Value
Type | Description |
---|---|
Boolean |
m_CachedNetworkManager
Internally used by NetworkTransform to keep track of the NetworkManager instance assigned to this this NetworkBehaviour derived class instance.
Declaration
protected NetworkManager m_CachedNetworkManager
Field Value
Type | Description |
---|---|
NetworkManager |
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 |
---|---|
NetworkTransform.OnClientRequestChangeDelegate |
PositionThreshold
The current position threshold value Any changes to the position that exceeds the current threshold value will be replicated
Declaration
public float PositionThreshold
Field Value
Type | Description |
---|---|
Single |
PositionThresholdDefault
The default position change threshold value. Any changes above this threshold will be replicated.
Declaration
public const float PositionThresholdDefault = 0.001F
Field Value
Type | Description |
---|---|
Single |
RotAngleThreshold
The current rotation threshold value Any changes to the rotation that exceeds the current threshold value will be replicated Minimum Value: 0.001 Maximum Value: 360.0
Declaration
[Range(0.001F, 360F)]
public float RotAngleThreshold
Field Value
Type | Description |
---|---|
Single |
RotAngleThresholdDefault
The default rotation angle change threshold value. Any changes above this threshold will be replicated.
Declaration
public const float RotAngleThresholdDefault = 0.01F
Field Value
Type | Description |
---|---|
Single |
ScaleThreshold
The current scale threshold value Any changes to the scale that exceeds the current threshold value will be replicated
Declaration
public float ScaleThreshold
Field Value
Type | Description |
---|---|
Single |
ScaleThresholdDefault
The default scale change threshold value. Any changes above this threshold will be replicated.
Declaration
public const float ScaleThresholdDefault = 0.01F
Field Value
Type | Description |
---|---|
Single |
SyncPositionX
Whether or not x component of position will be replicated
Declaration
public bool SyncPositionX
Field Value
Type | Description |
---|---|
Boolean |
SyncPositionY
Whether or not y component of position will be replicated
Declaration
public bool SyncPositionY
Field Value
Type | Description |
---|---|
Boolean |
SyncPositionZ
Whether or not z component of position will be replicated
Declaration
public bool SyncPositionZ
Field Value
Type | Description |
---|---|
Boolean |
SyncRotAngleX
Whether or not x component of rotation will be replicated
Declaration
public bool SyncRotAngleX
Field Value
Type | Description |
---|---|
Boolean |
SyncRotAngleY
Whether or not y component of rotation will be replicated
Declaration
public bool SyncRotAngleY
Field Value
Type | Description |
---|---|
Boolean |
SyncRotAngleZ
Whether or not z component of rotation will be replicated
Declaration
public bool SyncRotAngleZ
Field Value
Type | Description |
---|---|
Boolean |
SyncScaleX
Whether or not x component of scale will be replicated
Declaration
public bool SyncScaleX
Field Value
Type | Description |
---|---|
Boolean |
SyncScaleY
Whether or not y component of scale will be replicated
Declaration
public bool SyncScaleY
Field Value
Type | Description |
---|---|
Boolean |
SyncScaleZ
Whether or not z component of scale will be replicated
Declaration
public bool SyncScaleZ
Field Value
Type | Description |
---|---|
Boolean |
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 |
---|---|
Boolean |
Methods
OnDestroy()
Invoked when the GameObject the NetworkBehaviour is attached to. NOTE: If you override this, you will want to always invoke this base class version of this OnDestroy() method!!
Declaration
public override void OnDestroy()
Overrides
OnGainedOwnership()
Gets called when the local client gains ownership of this object
Declaration
public override void OnGainedOwnership()
Overrides
OnIsServerAuthoritative()
Override this method and return false to switch to owner authoritative mode
Declaration
protected virtual bool OnIsServerAuthoritative()
Returns
Type | Description |
---|---|
Boolean | ( |
OnLostOwnership()
Gets called when we loose ownership of this object
Declaration
public override void OnLostOwnership()
Overrides
OnNetworkDespawn()
Gets called when the NetworkObject gets despawned. Is called both on the server and clients.
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 setup.
Declaration
public override void OnNetworkSpawn()
Overrides
SetMaxInterpolationBound(Single)
Will set the maximum interpolation boundary for the interpolators of this NetworkTransform instance. This value roughly translates to the maximum value of 't' in Lerp and LerpUnclamped for all transform elements being monitored by NetworkTransform (i.e. Position, Rotation, and Scale)
Declaration
public void SetMaxInterpolationBound(float maxInterpolationBound)
Parameters
Type | Name | Description |
---|---|---|
Single | maxInterpolationBound | Maximum time boundary that can be used in a frame when interpolating between two values |
SetState(Nullable<Vector3>, Nullable<Quaternion>, Nullable<Vector3>, Boolean)
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 shouldGhostsInterpolate = true)
Parameters
Type | Name | Description |
---|---|---|
Nullable<Vector3> | posIn | |
Nullable<Quaternion> | rotIn | |
Nullable<Vector3> | scaleIn | new scale to scale to. Can be null |
Boolean | shouldGhostsInterpolate | Should other clients interpolate this change or not. True by default |
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-owners always invoke this base class method when using interpolation.
- Authority can opt to use TryCommitTransformToServer(Transform, Double) in place of invoking this base class method.
- Non-authority owners can use TryCommitTransformToServer(Transform, Double) but should still invoke the this base class method when using interpolation.