NetworkTransform
A class in Root.Township, Alta.Networking.
Syncs a transform's local position, rotation and optional scale from the authoritative side. Remote copies lerp toward it each fixed update, or snap when far off.
- Listed in Networking and servers
- Inherits or implements
NetworkEntityBehaviour,INetworkSerialized,IInitializeAsLocalPlayer - Attributes
ProfileNetworkSerialized
Constructors
| Member | What it is |
|---|---|
NetworkTransform() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
| Member | What it is |
|---|---|
static AccumulativeAverageCount prepareForWriteTimer | Shared timing stats for PrepareForWrite. |
static AccumulativeAverageCount serializeTimer | Shared timing stats for Serialize. |
24 private and internal fields
| Member | What it is |
|---|---|
private float angleThresholdDot | Rotation change threshold as a quaternion dot. A rotation closer than this to the last sent one doesn't count as a change. |
private IUnityUpdateMethod fixedUpdate | Handle for the registered HandleRemote fixed-update callback, or null when not lerping. |
private float fixedUpdateLerpTime | Lerp factor per fixed update on clients. At 1 or more the client snaps instead of lerping. Set by SetLerp. |
private bool isAlwaysSnapping | True on the server, or when the lerp factor is 1 or more: received values are applied with Snap every time. |
private bool isIncludingScale | Whether uniform scale (the x of local scale) is synced as well. |
private bool isSnapping | True while a Snap is queued for the end of the current receive. |
private bool isWorldBasedPosition | When true, position is sent with its chunk via Chunk.SerializeChunkAndPosition. When false, as a short local vector (up to 10 units). |
private float lastSyncTime | Time of the last sync, or 0 if none yet. Nothing is applied until the first sync arrives. |
private Vector3 latestPosition | Last position applied to the transform by the lerp or a snap. |
private Quaternion latestRotation | Last rotation applied to the transform by the lerp or a snap. |
private float latestScale | Last scale applied to the transform by the lerp or a snap. |
private static Logger logger | NLog logger for this class. |
private const float MaximumScale | Largest scale the 10-bit scale field can send (10.23). |
private float movementThresholdSquared | Squared distance the transform must move before a new position is sent. |
private Vector3 position | Position being sent, or the last one received. |
private Quaternion rotation | Rotation being sent, or the last one received. |
private float scale | Uniform scale being sent, or the last one received. |
private const int ScaleBits | Number of bits used to send the scale (10). |
private int scaleChangedCount | Countdown of syncs that still include the scale after it last changed. Reset to UnchangedResend. |
private float snapDistance | Distance from the received position past which the client snaps instead of lerping (default 2). |
private float snapDistanceSquared | Square of snapDistance, worked out in Initialize. |
private Transform targetTransform | Transform that is synced. Falls back to this object's own transform when empty. |
private const int UnchangedResend | How many more syncs keep sending the scale after it stops changing (30). |
private bool wasNullChunk | Server: set when the entity tried to sync with no chunk. A second time in a row destroys the entity. |
Properties
| Member | What it is |
|---|---|
Transform TargetTransform { get; set; } | The transform synced. Defaults to this object's own. |
3 private and internal properties
| Member | What it is |
|---|---|
private int Alta.Networking.INetworkSerialized.MaximumBits { get; } | Most bits one sync of this component can write (109). |
private AccumulativeAverageCount Alta.Networking.INetworkSerialized.PrepareForWriteTimer { get; } | Returns the static timer shared by all NetworkTransforms for profiling PrepareForWrite. |
private AccumulativeAverageCount Alta.Networking.INetworkSerialized.SerializeTimer { get; } | Returns the static timer shared by all NetworkTransforms for profiling Serialize. |
Methods
| Member | What it is |
|---|---|
override void Initialize() | On clients, starts the lerp update. The server always snaps. |
override void InitializeAsServer(SpawnData spawnData) | Server: no extra setup. |
bool PrepareForWrite(bool isWriteFull) | Records the current transform and returns whether it moved, turned or scaled past the thresholds. |
void Serialize(Stream stream, bool isWriteFull) | Writes or reads the position (with chunk when world based), rotation and scale. Schedules a snap when the gap is large. |
void SetLerp(float lerp) | Sets how much of the gap is closed each fixed update. PlayerLocomotionController sets it from the smoothing setting. |
5 private and internal methods
| Member | What it is |
|---|---|
private void HandleRemote() | Client: runs each fixed update and lerps the target transform toward the last received position, rotation and scale. |
private void IInitializeAsLocalPlayer.InitializeAsLocalPlayer(PlayerController player) | On the local player's own object, stops the remote lerp so synced values don't pull the player around. |
private void OnDisable() | Stops the fixed-update lerp. |
private void OnEnable() | Registers the fixed-update lerp again if the entity is initialized and this side doesn't always snap. |
private void Snap() | Moves the transform straight to the received values. Queued for the end of a receive when the gap is past snapDistance. |
Used in
Members of other types that take, return or hold a NetworkTransform: