Skip to main content

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.

Constructors​

MemberWhat it is
NetworkTransform()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

MemberWhat it is
static AccumulativeAverageCount prepareForWriteTimerShared timing stats for PrepareForWrite.
static AccumulativeAverageCount serializeTimerShared timing stats for Serialize.
24 private and internal fields
MemberWhat it is
private float angleThresholdDotRotation change threshold as a quaternion dot. A rotation closer than this to the last sent one doesn't count as a change.
private IUnityUpdateMethod fixedUpdateHandle for the registered HandleRemote fixed-update callback, or null when not lerping.
private float fixedUpdateLerpTimeLerp factor per fixed update on clients. At 1 or more the client snaps instead of lerping. Set by SetLerp.
private bool isAlwaysSnappingTrue on the server, or when the lerp factor is 1 or more: received values are applied with Snap every time.
private bool isIncludingScaleWhether uniform scale (the x of local scale) is synced as well.
private bool isSnappingTrue while a Snap is queued for the end of the current receive.
private bool isWorldBasedPositionWhen true, position is sent with its chunk via Chunk.SerializeChunkAndPosition. When false, as a short local vector (up to 10 units).
private float lastSyncTimeTime of the last sync, or 0 if none yet. Nothing is applied until the first sync arrives.
private Vector3 latestPositionLast position applied to the transform by the lerp or a snap.
private Quaternion latestRotationLast rotation applied to the transform by the lerp or a snap.
private float latestScaleLast scale applied to the transform by the lerp or a snap.
private static Logger loggerNLog logger for this class.
private const float MaximumScaleLargest scale the 10-bit scale field can send (10.23).
private float movementThresholdSquaredSquared distance the transform must move before a new position is sent.
private Vector3 positionPosition being sent, or the last one received.
private Quaternion rotationRotation being sent, or the last one received.
private float scaleUniform scale being sent, or the last one received.
private const int ScaleBitsNumber of bits used to send the scale (10).
private int scaleChangedCountCountdown of syncs that still include the scale after it last changed. Reset to UnchangedResend.
private float snapDistanceDistance from the received position past which the client snaps instead of lerping (default 2).
private float snapDistanceSquaredSquare of snapDistance, worked out in Initialize.
private Transform targetTransformTransform that is synced. Falls back to this object's own transform when empty.
private const int UnchangedResendHow many more syncs keep sending the scale after it stops changing (30).
private bool wasNullChunkServer: set when the entity tried to sync with no chunk. A second time in a row destroys the entity.

Properties​

MemberWhat it is
Transform TargetTransform { get; set; }The transform synced. Defaults to this object's own.
3 private and internal properties
MemberWhat 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​

MemberWhat 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
MemberWhat 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: