Class NetworkTransform | Multiplayer HLAPI | 1.0.8
docs.unity.cn
    Show / Hide Table of Contents

    Class NetworkTransform

    A component to synchronize the position and rotation of networked objects.

    The movement of game objects can be networked by this component. There are two models of authority for networked movement:

    If the object has authority on the client, then it should be controlled locally on the owning client, then movement state information will be sent from the owning client to the server, then broadcast to all of the other clients. This is common for player objects.

    If the object has authority on the server, then it should be controlled on the server and movement state information will be sent to all clients. This is common for objects not related to a specific client, such as an enemy unit.

    Inheritance
    Object
    Object
    Component
    Behaviour
    MonoBehaviour
    NetworkBehaviour
    NetworkTransform
    Inherited Members
    NetworkBehaviour.localPlayerAuthority
    NetworkBehaviour.isServer
    NetworkBehaviour.isClient
    NetworkBehaviour.isLocalPlayer
    NetworkBehaviour.hasAuthority
    NetworkBehaviour.netId
    NetworkBehaviour.connectionToServer
    NetworkBehaviour.connectionToClient
    NetworkBehaviour.playerControllerId
    NetworkBehaviour.syncVarDirtyBits
    NetworkBehaviour.syncVarHookGuard
    NetworkBehaviour.SendCommandInternal(NetworkWriter, Int32, String)
    NetworkBehaviour.InvokeCommand(Int32, NetworkReader)
    NetworkBehaviour.SendRPCInternal(NetworkWriter, Int32, String)
    NetworkBehaviour.SendTargetRPCInternal(NetworkConnection, NetworkWriter, Int32, String)
    NetworkBehaviour.InvokeRPC(Int32, NetworkReader)
    NetworkBehaviour.SendEventInternal(NetworkWriter, Int32, String)
    NetworkBehaviour.InvokeSyncEvent(Int32, NetworkReader)
    NetworkBehaviour.InvokeSyncList(Int32, NetworkReader)
    NetworkBehaviour.RegisterCommandDelegate(Type, Int32, NetworkBehaviour.CmdDelegate)
    NetworkBehaviour.RegisterRpcDelegate(Type, Int32, NetworkBehaviour.CmdDelegate)
    NetworkBehaviour.RegisterEventDelegate(Type, Int32, NetworkBehaviour.CmdDelegate)
    NetworkBehaviour.RegisterSyncListDelegate(Type, Int32, NetworkBehaviour.CmdDelegate)
    NetworkBehaviour.SetSyncVarGameObject(GameObject, GameObject, UInt32, NetworkInstanceId)
    NetworkBehaviour.SetSyncVar<T>(T, T, UInt32)
    NetworkBehaviour.SetDirtyBit(UInt32)
    NetworkBehaviour.ClearAllDirtyBits()
    NetworkBehaviour.PreStartClient()
    NetworkBehaviour.OnNetworkDestroy()
    NetworkBehaviour.OnStartClient()
    NetworkBehaviour.OnStartLocalPlayer()
    NetworkBehaviour.OnStopAuthority()
    NetworkBehaviour.OnRebuildObservers(HashSet<NetworkConnection>, Boolean)
    NetworkBehaviour.OnSetLocalVisibility(Boolean)
    NetworkBehaviour.OnCheckObserver(NetworkConnection)
    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.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
    Component.rigidbody
    Component.camera
    Component.light
    Component.animation
    Component.constantForce
    Component.renderer
    Component.audio
    UnityEngine.Component.guiText
    Component.networkView
    UnityEngine.Component.guiElement
    UnityEngine.Component.guiTexture
    Component.collider
    Component.collider2D
    Component.hingeJoint
    Component.particleSystem
    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
    Namespace: UnityEngine.Networking
    Syntax
    [DisallowMultipleComponent]
    [AddComponentMenu("Network/NetworkTransform")]
    [Obsolete("The high level API classes are deprecated and will be removed in the future.")]
    public class NetworkTransform : NetworkBehaviour

    Properties

    characterContoller

    Cached CharacterController.

    Declaration
    public CharacterController characterContoller { get; }
    Property Value
    Type Description
    CharacterController

    clientMoveCallback2D

    A callback that can be used to validate on the server, the movement of client authoritative objects.

    This version of the callback works with objects that use 2D physics. The callback function may return false to reject the movement request completely. It may also modify the movement parameters - which are passed by reference.

    The example below set the callback in OnStartServer, and will disconnect a client that moves an object into an invalid position after a number of failures.

    using UnityEngine;
    using UnityEngine.Networking;
    
    public class MyMover : NetworkManager
    {
       public int cheatCount = 0;
    
       public bool ValidateMove(ref Vector2 position, ref Vector2 velocity, ref float rotation)
       {
           Debug.Log("pos:" + position);
           if (position.y > 9)
           {
               position.y = 9;
               cheatCount += 1;
               if (cheatCount == 10)
               {
                   Invoke("DisconnectCheater", 0.1f);
               }
           }
           return true;
       }
    
       void DisconnectCheater()
       {
           GetComponent<NetworkIdentity>().connectionToClient.Disconnect();
       }
    
       public override void OnStartServer()
       {
           GetComponent<NetworkTransform>().clientMoveCallback2D = ValidateMove;
       }
    }

    This kind of server-side movement validation should be used in conjunction with client side movement validation. The callback should only detect a failure if a client is by-passing client side movement checks - by cheating.

    Declaration
    public NetworkTransform.ClientMoveCallback2D clientMoveCallback2D { get; set; }
    Property Value
    Type Description
    NetworkTransform.ClientMoveCallback2D

    clientMoveCallback3D

    A callback that can be used to validate on the server, the movement of client authoritative objects.

    This version of the callback works with objects that use 3D physics. The callback function may return false to reject the movement request completely. It may also modify the movement parameters - which are passed by reference.

    The example below set the callback in OnStartServer, and will disconnect a client that moves an object into an invalid position after a number of failures.

    using UnityEngine;
    using UnityEngine.Networking;
    
    public class MyMover : NetworkManager
    {
       public int cheatCount = 0;
    
       public bool ValidateMove(ref Vector3 position, ref Vector3 velocity, ref Quaternion rotation)
       {
           Debug.Log("pos:" + position);
           if (position.y > 9)
           {
               position.y = 9;
               cheatCount += 1;
               if (cheatCount == 10)
               {
                   Invoke("DisconnectCheater", 0.1f);
               }
           }
           return true;
       }
    
       void DisconnectCheater()
       {
           GetComponent<NetworkIdentity>().connectionToClient.Disconnect();
       }
    
       public override void OnStartServer()
       {
           GetComponent<NetworkTransform>().clientMoveCallback3D = ValidateMove;
       }
    }

    This kind of server-side movement validation should be used in conjunction with client side movement validation. The callback should only detect a failure if a client is by-passing client side movement checks - by cheating.

    Declaration
    public NetworkTransform.ClientMoveCallback3D clientMoveCallback3D { get; set; }
    Property Value
    Type Description
    NetworkTransform.ClientMoveCallback3D

    grounded

    Tells the NetworkTransform that it is on a surface (this is the default).

    Object that are NOT grounded will not interpolate their vertical velocity. This avoid the problem of interpolation fighting with gravity on non-authoritative objects. This only works for RigidBody2D physics objects.

    Declaration
    public bool grounded { get; set; }
    Property Value
    Type Description
    Boolean

    interpolateMovement

    Enables interpolation of the synchronized movement.

    The larger this number is, the faster the object will interpolate to the target position.

    Declaration
    public float interpolateMovement { get; set; }
    Property Value
    Type Description
    Single

    interpolateRotation

    Enables interpolation of the synchronized rotation.

    If this is not set, object will snap to the new rotation. The larger this number is, the faster the object will interpolate to the target facing direction.

    Declaration
    public float interpolateRotation { get; set; }
    Property Value
    Type Description
    Single

    lastSyncTime

    The most recent time when a movement synchronization packet arrived for this object.

    Declaration
    public float lastSyncTime { get; }
    Property Value
    Type Description
    Single

    movementTheshold

    The distance that an object can move without sending a movement synchronization update.

    Declaration
    public float movementTheshold { get; set; }
    Property Value
    Type Description
    Single

    rigidbody2D

    Cached Rigidbody2D.

    Declaration
    public Rigidbody2D rigidbody2D { get; }
    Property Value
    Type Description
    Rigidbody2D

    rigidbody3D

    Cached Rigidbody.

    Declaration
    public Rigidbody rigidbody3D { get; }
    Property Value
    Type Description
    Rigidbody

    rotationSyncCompression

    How much to compress rotation sync updates.

    Declaration
    public NetworkTransform.CompressionSyncMode rotationSyncCompression { get; set; }
    Property Value
    Type Description
    NetworkTransform.CompressionSyncMode

    sendInterval

    The sendInterval controls how often state updates are sent for this object.

    Unlike most NetworkBehaviour scripts, for NetworkTransform this is implemented at a per-object level rather than at the per-script level. This allows more flexibility as this component is used in various situation.

    If sendInterval is non-zero, then transform state updates are send at most once every sendInterval seconds. However, if an object is stationary, no updates are sent.

    If sendInterval is zero, then no automatic updates are sent. In this case, calling SetDirtyBits() on the NetworkTransform will cause an updates to be sent. This could be used for objects like bullets that have a predictable trajectory.

    Declaration
    public float sendInterval { get; set; }
    Property Value
    Type Description
    Single

    snapThreshold

    If a movement update puts an object further from its current position that this value, it will snap to the position instead of moving smoothly.

    Declaration
    public float snapThreshold { get; set; }
    Property Value
    Type Description
    Single

    syncRotationAxis

    Which axis should rotation by synchronized for.

    Declaration
    public NetworkTransform.AxisSyncMode syncRotationAxis { get; set; }
    Property Value
    Type Description
    NetworkTransform.AxisSyncMode

    syncSpin

    Declaration
    public bool syncSpin { get; set; }
    Property Value
    Type Description
    Boolean

    targetSyncPosition

    The target position interpolating towards.

    Declaration
    public Vector3 targetSyncPosition { get; }
    Property Value
    Type Description
    Vector3

    targetSyncRotation2D

    The target rotation interpolating towards.

    Declaration
    public float targetSyncRotation2D { get; }
    Property Value
    Type Description
    Single

    targetSyncRotation3D

    The target position interpolating towards.

    Declaration
    public Quaternion targetSyncRotation3D { get; }
    Property Value
    Type Description
    Quaternion

    targetSyncVelocity

    The velocity send for synchronization.

    Declaration
    public Vector3 targetSyncVelocity { get; }
    Property Value
    Type Description
    Vector3

    transformSyncMode

    What method to use to sync the object's position.

    Declaration
    public NetworkTransform.TransformSyncMode transformSyncMode { get; set; }
    Property Value
    Type Description
    NetworkTransform.TransformSyncMode

    velocityThreshold

    The minimum velocity difference that will be synchronized over the network.

    Declaration
    public float velocityThreshold { get; set; }
    Property Value
    Type Description
    Single

    Methods

    GetNetworkChannel()

    Declaration
    public override int GetNetworkChannel()
    Returns
    Type Description
    Int32
    Overrides
    NetworkBehaviour.GetNetworkChannel()

    GetNetworkSendInterval()

    Declaration
    public override float GetNetworkSendInterval()
    Returns
    Type Description
    Single
    Overrides
    NetworkBehaviour.GetNetworkSendInterval()

    HandleTransform(NetworkMessage)

    Declaration
    public static void HandleTransform(NetworkMessage netMsg)
    Parameters
    Type Name Description
    NetworkMessage netMsg

    OnDeserialize(NetworkReader, Boolean)

    Declaration
    public override void OnDeserialize(NetworkReader reader, bool initialState)
    Parameters
    Type Name Description
    NetworkReader reader
    Boolean initialState
    Overrides
    NetworkBehaviour.OnDeserialize(NetworkReader, Boolean)

    OnSerialize(NetworkWriter, Boolean)

    Declaration
    public override bool OnSerialize(NetworkWriter writer, bool initialState)
    Parameters
    Type Name Description
    NetworkWriter writer
    Boolean initialState
    Returns
    Type Description
    Boolean
    Overrides
    NetworkBehaviour.OnSerialize(NetworkWriter, Boolean)

    OnStartAuthority()

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

    OnStartServer()

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

    SerializeRotation2D(NetworkWriter, Single, NetworkTransform.CompressionSyncMode)

    Declaration
    public static void SerializeRotation2D(NetworkWriter writer, float rot, NetworkTransform.CompressionSyncMode compression)
    Parameters
    Type Name Description
    NetworkWriter writer
    Single rot
    NetworkTransform.CompressionSyncMode compression

    SerializeRotation3D(NetworkWriter, Quaternion, NetworkTransform.AxisSyncMode, NetworkTransform.CompressionSyncMode)

    Declaration
    public static void SerializeRotation3D(NetworkWriter writer, Quaternion rot, NetworkTransform.AxisSyncMode mode, NetworkTransform.CompressionSyncMode compression)
    Parameters
    Type Name Description
    NetworkWriter writer
    Quaternion rot
    NetworkTransform.AxisSyncMode mode
    NetworkTransform.CompressionSyncMode compression

    SerializeSpin2D(NetworkWriter, Single, NetworkTransform.CompressionSyncMode)

    Declaration
    public static void SerializeSpin2D(NetworkWriter writer, float angularVelocity, NetworkTransform.CompressionSyncMode compression)
    Parameters
    Type Name Description
    NetworkWriter writer
    Single angularVelocity
    NetworkTransform.CompressionSyncMode compression

    SerializeSpin3D(NetworkWriter, Vector3, NetworkTransform.AxisSyncMode, NetworkTransform.CompressionSyncMode)

    Declaration
    public static void SerializeSpin3D(NetworkWriter writer, Vector3 angularVelocity, NetworkTransform.AxisSyncMode mode, NetworkTransform.CompressionSyncMode compression)
    Parameters
    Type Name Description
    NetworkWriter writer
    Vector3 angularVelocity
    NetworkTransform.AxisSyncMode mode
    NetworkTransform.CompressionSyncMode compression

    SerializeVelocity2D(NetworkWriter, Vector2, NetworkTransform.CompressionSyncMode)

    Declaration
    public static void SerializeVelocity2D(NetworkWriter writer, Vector2 velocity, NetworkTransform.CompressionSyncMode compression)
    Parameters
    Type Name Description
    NetworkWriter writer
    Vector2 velocity
    NetworkTransform.CompressionSyncMode compression

    SerializeVelocity3D(NetworkWriter, Vector3, NetworkTransform.CompressionSyncMode)

    Declaration
    public static void SerializeVelocity3D(NetworkWriter writer, Vector3 velocity, NetworkTransform.CompressionSyncMode compression)
    Parameters
    Type Name Description
    NetworkWriter writer
    Vector3 velocity
    NetworkTransform.CompressionSyncMode compression

    UnserializeRotation2D(NetworkReader, NetworkTransform.CompressionSyncMode)

    Declaration
    public static float UnserializeRotation2D(NetworkReader reader, NetworkTransform.CompressionSyncMode compression)
    Parameters
    Type Name Description
    NetworkReader reader
    NetworkTransform.CompressionSyncMode compression
    Returns
    Type Description
    Single

    UnserializeRotation3D(NetworkReader, NetworkTransform.AxisSyncMode, NetworkTransform.CompressionSyncMode)

    Declaration
    public static Quaternion UnserializeRotation3D(NetworkReader reader, NetworkTransform.AxisSyncMode mode, NetworkTransform.CompressionSyncMode compression)
    Parameters
    Type Name Description
    NetworkReader reader
    NetworkTransform.AxisSyncMode mode
    NetworkTransform.CompressionSyncMode compression
    Returns
    Type Description
    Quaternion

    UnserializeSpin2D(NetworkReader, NetworkTransform.CompressionSyncMode)

    Declaration
    public static float UnserializeSpin2D(NetworkReader reader, NetworkTransform.CompressionSyncMode compression)
    Parameters
    Type Name Description
    NetworkReader reader
    NetworkTransform.CompressionSyncMode compression
    Returns
    Type Description
    Single

    UnserializeSpin3D(NetworkReader, NetworkTransform.AxisSyncMode, NetworkTransform.CompressionSyncMode)

    Declaration
    public static Vector3 UnserializeSpin3D(NetworkReader reader, NetworkTransform.AxisSyncMode mode, NetworkTransform.CompressionSyncMode compression)
    Parameters
    Type Name Description
    NetworkReader reader
    NetworkTransform.AxisSyncMode mode
    NetworkTransform.CompressionSyncMode compression
    Returns
    Type Description
    Vector3

    UnserializeVelocity2D(NetworkReader, NetworkTransform.CompressionSyncMode)

    Declaration
    public static Vector3 UnserializeVelocity2D(NetworkReader reader, NetworkTransform.CompressionSyncMode compression)
    Parameters
    Type Name Description
    NetworkReader reader
    NetworkTransform.CompressionSyncMode compression
    Returns
    Type Description
    Vector3

    UnserializeVelocity3D(NetworkReader, NetworkTransform.CompressionSyncMode)

    Declaration
    public static Vector3 UnserializeVelocity3D(NetworkReader reader, NetworkTransform.CompressionSyncMode compression)
    Parameters
    Type Name Description
    NetworkReader reader
    NetworkTransform.CompressionSyncMode compression
    Returns
    Type Description
    Vector3
    In This Article
    • Properties
      • characterContoller
      • clientMoveCallback2D
      • clientMoveCallback3D
      • grounded
      • interpolateMovement
      • interpolateRotation
      • lastSyncTime
      • movementTheshold
      • rigidbody2D
      • rigidbody3D
      • rotationSyncCompression
      • sendInterval
      • snapThreshold
      • syncRotationAxis
      • syncSpin
      • targetSyncPosition
      • targetSyncRotation2D
      • targetSyncRotation3D
      • targetSyncVelocity
      • transformSyncMode
      • velocityThreshold
    • Methods
      • GetNetworkChannel()
      • GetNetworkSendInterval()
      • HandleTransform(NetworkMessage)
      • OnDeserialize(NetworkReader, Boolean)
      • OnSerialize(NetworkWriter, Boolean)
      • OnStartAuthority()
      • OnStartServer()
      • SerializeRotation2D(NetworkWriter, Single, NetworkTransform.CompressionSyncMode)
      • SerializeRotation3D(NetworkWriter, Quaternion, NetworkTransform.AxisSyncMode, NetworkTransform.CompressionSyncMode)
      • SerializeSpin2D(NetworkWriter, Single, NetworkTransform.CompressionSyncMode)
      • SerializeSpin3D(NetworkWriter, Vector3, NetworkTransform.AxisSyncMode, NetworkTransform.CompressionSyncMode)
      • SerializeVelocity2D(NetworkWriter, Vector2, NetworkTransform.CompressionSyncMode)
      • SerializeVelocity3D(NetworkWriter, Vector3, NetworkTransform.CompressionSyncMode)
      • UnserializeRotation2D(NetworkReader, NetworkTransform.CompressionSyncMode)
      • UnserializeRotation3D(NetworkReader, NetworkTransform.AxisSyncMode, NetworkTransform.CompressionSyncMode)
      • UnserializeSpin2D(NetworkReader, NetworkTransform.CompressionSyncMode)
      • UnserializeSpin3D(NetworkReader, NetworkTransform.AxisSyncMode, NetworkTransform.CompressionSyncMode)
      • UnserializeVelocity2D(NetworkReader, NetworkTransform.CompressionSyncMode)
      • UnserializeVelocity3D(NetworkReader, NetworkTransform.CompressionSyncMode)
    Back to top Copyright © 2020 Unity Technologies
    Generated by DocFX
    on 15 December 2020