docs.unity3d.com
    Show / Hide Table of Contents

    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.

    Inheritance
    Object
    Object
    Component
    Behaviour
    MonoBehaviour
    NetworkBehaviour
    NetworkTransform
    NetworkTransformTestComponent
    Inherited Members
    NetworkBehaviour.NetworkManager
    NetworkBehaviour.IsLocalPlayer
    NetworkBehaviour.IsOwner
    NetworkBehaviour.IsServer
    NetworkBehaviour.IsClient
    NetworkBehaviour.IsHost
    NetworkBehaviour.IsOwnedByServer
    NetworkBehaviour.IsSpawned
    NetworkBehaviour.NetworkObject
    NetworkBehaviour.HasNetworkObject
    NetworkBehaviour.NetworkObjectId
    NetworkBehaviour.NetworkBehaviourId
    NetworkBehaviour.GetNetworkBehaviour(UInt16)
    NetworkBehaviour.OwnerClientId
    NetworkBehaviour.OnNetworkObjectParentChanged(NetworkObject)
    NetworkBehaviour.GetNetworkObject(UInt64)
    MonoBehaviour.IsInvoking()
    MonoBehaviour.CancelInvoke()
    MonoBehaviour.Invoke(String, Single)
    MonoBehaviour.InvokeRepeating(String, Single, Single)
    MonoBehaviour.CancelInvoke(String)
    MonoBehaviour.IsInvoking(String)
    MonoBehaviour.StartCoroutine(String)
    MonoBehaviour.StartCoroutine(String, Object)
    MonoBehaviour.StartCoroutine(IEnumerator)
    MonoBehaviour.StartCoroutine_Auto(IEnumerator)
    MonoBehaviour.StopCoroutine(IEnumerator)
    MonoBehaviour.StopCoroutine(Coroutine)
    MonoBehaviour.StopCoroutine(String)
    MonoBehaviour.StopAllCoroutines()
    MonoBehaviour.print(Object)
    MonoBehaviour.useGUILayout
    MonoBehaviour.runInEditMode
    Behaviour.enabled
    Behaviour.isActiveAndEnabled
    Component.GetComponent(Type)
    Component.GetComponent<T>()
    Component.TryGetComponent(Type, Component)
    Component.TryGetComponent<T>(T)
    Component.GetComponent(String)
    Component.GetComponentInChildren(Type, Boolean)
    Component.GetComponentInChildren(Type)
    Component.GetComponentInChildren<T>(Boolean)
    Component.GetComponentInChildren<T>()
    Component.GetComponentsInChildren(Type, Boolean)
    Component.GetComponentsInChildren(Type)
    Component.GetComponentsInChildren<T>(Boolean)
    Component.GetComponentsInChildren<T>(Boolean, List<T>)
    Component.GetComponentsInChildren<T>()
    Component.GetComponentsInChildren<T>(List<T>)
    Component.GetComponentInParent(Type)
    Component.GetComponentInParent<T>()
    Component.GetComponentsInParent(Type, Boolean)
    Component.GetComponentsInParent(Type)
    Component.GetComponentsInParent<T>(Boolean)
    Component.GetComponentsInParent<T>(Boolean, List<T>)
    Component.GetComponentsInParent<T>()
    Component.GetComponents(Type)
    Component.GetComponents(Type, List<Component>)
    Component.GetComponents<T>(List<T>)
    Component.GetComponents<T>()
    Component.CompareTag(String)
    Component.SendMessageUpwards(String, Object, SendMessageOptions)
    Component.SendMessageUpwards(String, Object)
    Component.SendMessageUpwards(String)
    Component.SendMessageUpwards(String, SendMessageOptions)
    Component.SendMessage(String, Object)
    Component.SendMessage(String)
    Component.SendMessage(String, Object, SendMessageOptions)
    Component.SendMessage(String, SendMessageOptions)
    Component.BroadcastMessage(String, Object, SendMessageOptions)
    Component.BroadcastMessage(String, Object)
    Component.BroadcastMessage(String)
    Component.BroadcastMessage(String, SendMessageOptions)
    Component.transform
    Component.gameObject
    Component.tag
    Object.GetInstanceID()
    Object.GetHashCode()
    Object.Equals(Object)
    Object.Instantiate(Object, Vector3, Quaternion)
    Object.Instantiate(Object, Vector3, Quaternion, Transform)
    Object.Instantiate(Object)
    Object.Instantiate(Object, Transform)
    Object.Instantiate(Object, Transform, Boolean)
    Object.Instantiate<T>(T)
    Object.Instantiate<T>(T, Vector3, Quaternion)
    Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
    Object.Instantiate<T>(T, Transform)
    Object.Instantiate<T>(T, Transform, Boolean)
    Object.Destroy(Object, Single)
    Object.Destroy(Object)
    Object.DestroyImmediate(Object, Boolean)
    Object.DestroyImmediate(Object)
    Object.FindObjectsOfType(Type)
    Object.DontDestroyOnLoad(Object)
    Object.DestroyObject(Object, Single)
    Object.DestroyObject(Object)
    Object.FindSceneObjectsOfType(Type)
    Object.FindObjectsOfTypeIncludingAssets(Type)
    Object.FindObjectsOfType<T>()
    Object.FindObjectOfType<T>()
    Object.FindObjectsOfTypeAll(Type)
    Object.FindObjectOfType(Type)
    Object.ToString()
    Object.name
    Object.hideFlags
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: Unity.Netcode.Components
    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
    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
    NetworkBehaviour.OnDestroy()

    OnGainedOwnership()

    Gets called when the local client gains ownership of this object

    Declaration
    public override void OnGainedOwnership()
    Overrides
    NetworkBehaviour.OnGainedOwnership()

    OnIsServerAuthoritative()

    Override this method and return false to switch to owner authoritative mode

    Declaration
    protected virtual bool OnIsServerAuthoritative()
    Returns
    Type Description
    Boolean

    ( or ) where when false it runs as owner-client authoritative

    OnLostOwnership()

    Gets called when we loose ownership of this object

    Declaration
    public override void OnLostOwnership()
    Overrides
    NetworkBehaviour.OnLostOwnership()

    OnNetworkDespawn()

    Gets called when the NetworkObject gets despawned. Is called both on the server and clients.

    Declaration
    public override void OnNetworkDespawn()
    Overrides
    NetworkBehaviour.OnNetworkDespawn()

    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
    NetworkBehaviour.OnNetworkSpawn()

    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
    BufferSerializer<T> serializer
    Type Parameters
    Name Description
    T
    Overrides
    Unity.Netcode.NetworkBehaviour.OnSynchronize<T>(Unity.Netcode.BufferSerializer<T>)

    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 teleportDisabled = true)
    Parameters
    Type Name Description
    Nullable<Vector3> posIn
    Nullable<Quaternion> rotIn
    Nullable<Vector3> scaleIn

    new scale to scale to. Can be null

    Boolean teleportDisabled

    When true (the default) the NetworkObject will not be teleported and, if enabled, will interpolate. When false the NetworkObject will teleport/apply the parameters provided immediately.

    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.
    In This Article
    • Fields
      • InLocalSpace
      • Interpolate
      • m_CachedIsServer
      • m_CachedNetworkManager
      • OnClientRequestChange
      • PositionThreshold
      • PositionThresholdDefault
      • RotAngleThreshold
      • RotAngleThresholdDefault
      • ScaleThreshold
      • ScaleThresholdDefault
      • SyncPositionX
      • SyncPositionY
      • SyncPositionZ
      • SyncRotAngleX
      • SyncRotAngleY
      • SyncRotAngleZ
      • SyncScaleX
      • SyncScaleY
      • SyncScaleZ
    • Properties
      • CanCommitToTransform
    • Methods
      • OnDestroy()
      • OnGainedOwnership()
      • OnIsServerAuthoritative()
      • OnLostOwnership()
      • OnNetworkDespawn()
      • OnNetworkSpawn()
      • OnSynchronize<T>(ref BufferSerializer<T>)
      • SetMaxInterpolationBound(Single)
      • SetState(Nullable<Vector3>, Nullable<Quaternion>, Nullable<Vector3>, Boolean)
      • Teleport(Vector3, Quaternion, Vector3)
      • TryCommitTransformToServer(Transform, Double)
      • Update()
    Back to top Copyright © 2023 Unity Technologies — Terms of use
    Generated by DocFX
    on 26 October 2023