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
NetworkTransform
Inherited Members
UnityEngine.Component.guiText
UnityEngine.Component.guiElement
UnityEngine.Component.guiTexture
[DisallowMultipleComponent]
[AddComponentMenu("Network/NetworkTransform")]
[Obsolete("The high level API classes are deprecated and will be removed in the future.")]
public class NetworkTransform : NetworkBehaviour
Properties
Cached CharacterController.
Declaration
public CharacterController characterContoller { get; }
Property Value
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
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
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
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
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
The most recent time when a movement synchronization packet arrived for this object.
Declaration
public float lastSyncTime { get; }
Property Value
The distance that an object can move without sending a movement synchronization update.
Declaration
public float movementTheshold { get; set; }
Property Value
rigidbody2D
Declaration
public Rigidbody2D rigidbody2D { get; }
Property Value
rigidbody3D
Declaration
public Rigidbody rigidbody3D { get; }
Property Value
How much to compress rotation sync updates.
Declaration
public NetworkTransform.CompressionSyncMode rotationSyncCompression { get; set; }
Property Value
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
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
Which axis should rotation by synchronized for.
Declaration
public NetworkTransform.AxisSyncMode syncRotationAxis { get; set; }
Property Value
Declaration
public bool syncSpin { get; set; }
Property Value
The target position interpolating towards.
Declaration
public Vector3 targetSyncPosition { get; }
Property Value
The target rotation interpolating towards.
Declaration
public float targetSyncRotation2D { get; }
Property Value
The target position interpolating towards.
Declaration
public Quaternion targetSyncRotation3D { get; }
Property Value
The velocity send for synchronization.
Declaration
public Vector3 targetSyncVelocity { get; }
Property Value
What method to use to sync the object's position.
Declaration
public NetworkTransform.TransformSyncMode transformSyncMode { get; set; }
Property Value
The minimum velocity difference that will be synchronized over the network.
Declaration
public float velocityThreshold { get; set; }
Property Value
Methods
Declaration
public override int GetNetworkChannel()
Returns
Overrides
Declaration
public override float GetNetworkSendInterval()
Returns
Overrides
Declaration
public static void HandleTransform(NetworkMessage netMsg)
Parameters
Declaration
public override void OnDeserialize(NetworkReader reader, bool initialState)
Parameters
Overrides
Declaration
public override bool OnSerialize(NetworkWriter writer, bool initialState)
Parameters
Returns
Overrides
Declaration
public override void OnStartAuthority()
Overrides
Declaration
public override void OnStartServer()
Overrides
Declaration
public static void SerializeRotation2D(NetworkWriter writer, float rot, NetworkTransform.CompressionSyncMode compression)
Parameters
Declaration
public static void SerializeRotation3D(NetworkWriter writer, Quaternion rot, NetworkTransform.AxisSyncMode mode, NetworkTransform.CompressionSyncMode compression)
Parameters
Declaration
public static void SerializeSpin2D(NetworkWriter writer, float angularVelocity, NetworkTransform.CompressionSyncMode compression)
Parameters
Declaration
public static void SerializeSpin3D(NetworkWriter writer, Vector3 angularVelocity, NetworkTransform.AxisSyncMode mode, NetworkTransform.CompressionSyncMode compression)
Parameters
Declaration
public static void SerializeVelocity2D(NetworkWriter writer, Vector2 velocity, NetworkTransform.CompressionSyncMode compression)
Parameters
Declaration
public static void SerializeVelocity3D(NetworkWriter writer, Vector3 velocity, NetworkTransform.CompressionSyncMode compression)
Parameters
Declaration
public static float UnserializeRotation2D(NetworkReader reader, NetworkTransform.CompressionSyncMode compression)
Parameters
Returns
Declaration
public static Quaternion UnserializeRotation3D(NetworkReader reader, NetworkTransform.AxisSyncMode mode, NetworkTransform.CompressionSyncMode compression)
Parameters
Returns
Declaration
public static float UnserializeSpin2D(NetworkReader reader, NetworkTransform.CompressionSyncMode compression)
Parameters
Returns
Declaration
public static Vector3 UnserializeSpin3D(NetworkReader reader, NetworkTransform.AxisSyncMode mode, NetworkTransform.CompressionSyncMode compression)
Parameters
Returns
Declaration
public static Vector3 UnserializeVelocity2D(NetworkReader reader, NetworkTransform.CompressionSyncMode compression)
Parameters
Returns
Declaration
public static Vector3 UnserializeVelocity3D(NetworkReader reader, NetworkTransform.CompressionSyncMode compression)
Parameters
Returns