Skip to main content

NetworkRigidbody

A class in Root.Township, Alta.Networking.

Syncs a physics object's position, rotation, velocity, kinematic and sleep state from the server to clients, and saves them. On clients it eases the local rigidbody towards the server's values and sleeps it when updates stop. On almost every physics prefab.

Constructors​

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

Fields​

MemberWhat it is
static bool HasSecondaryPhysicsGlobal switch for relaying client-side collision results. False, so those messages are ignored.
bool isSuspendedPublic flag that nothing in the server build reads or sets.
static AccumulativeAverageCount prepareForWriteTimerShared profiling timer for PrepareForWrite.
static AccumulativeAverageCount serializeTimerShared profiling timer for Serialize.
20 private and internal fields
MemberWhat it is
private Vector3 angularVelocityLast synced angular velocity. Clients blend toward it.
private const float BaseLerpNormal blend factor (0.3) used to pull a client body toward the synced state each fixed update.
private Transform cachedTransformCached transform of the body.
private IUnityUpdateMethod clientUpdateHandle for the client fixed-update method in updateModule.
private bool isAsleepWhether this client body is counted as asleep (not in AwakeNetworkRigidbodies).
private bool isDestroyedTrue when the entity has no Rigidbody, or after DestroyRigidbody.
private bool isEntitiesChunkFullyLoadedClient: whether the chunk the body spawned in has fully loaded.
private bool isScaleChangedNot used in this build.
private bool isServerSleepingWhether the server's body was asleep in the last sync.
private float lastSyncTimeTime of the last sync. 0 means none yet. After 2 seconds with no sync the client body is put to sleep.
private float lerpCurrent blend factor toward the synced state. Lowered for a while after the client hands back authority.
private static Logger loggerNLog logger for this class.
private PositionSerializer<NetworkRigidbody> positionSerializerWrites and reads the body's position and rotation, chunk relative.
private LocationChunk spawnedInChunkClient: the chunk the body spawned in, waited on before sleeping.
private bool syncKinematicSynced kinematic state. While true, the client doesn't blend physics.
private MethodSyncClass<NetworkRigidbody> syncSecondaryCollisionMethod sync made in Initialize for SecondaryPhysics.
private static UpdateModule updateModuleFixed update module that runs ClientUpdate for every active client body.
private Vector3 velocityLast synced velocity. Clients blend toward it.
private bool wasKinematicKinematic state at the last write, used to spot changes.
private bool wasSleepingSleep state at the last write. A body that stays asleep and unchanged isn't resent.

Properties​

MemberWhat it is
static int AwakeNetworkRigidbodies { get; private set; }Count of network rigidbodies not asleep, across the scene.
bool IsActive { get; private set; }Client: true while the client update is running.
IMovementContext MovementContext { get; set; }Context that synced positions are relative to.
Vector3 Position { get; }Last synced position.
Quaternion Rotation { get; }Last synced rotation.
static int SpawnedCount { get; private set; }Number of network rigidbodies in the scene.
Rigidbody Topic { get; private set; }The Unity rigidbody being synced.
3 private and internal properties
MemberWhat it is
private int Alta.Networking.INetworkSerialized.MaximumBits { get; }Caps this behaviour's serialized data at 265 bits.
private AccumulativeAverageCount Alta.Networking.INetworkSerialized.PrepareForWriteTimer { get; }Returns the shared prepareForWriteTimer profiling counter.
private AccumulativeAverageCount Alta.Networking.INetworkSerialized.SerializeTimer { get; }Returns the shared serializeTimer profiling counter.

Methods​

MemberWhat it is
void ClientUpdate()Client, each fixed update: eases the body towards the synced values, or snaps and sleeps it after 2 seconds without an update.
void DestroyRigidbody()Destroys the Unity rigidbody and stops updating.
override void Initialize()Sets up the position serializer and messages. Clients wait for the chunk to load before handling updates.
void LerpAuthorityFromClient(float initialLerp, float duration)Lowers the easing strength for a while, then ramps it back up to the normal 0.3.
bool PrepareForWrite(bool isWriteFull)Server: reports a change unless the body was and still is asleep with the same kinematic state.
void Reset()Forgets the last sync time, so the next full update snaps the body into place.
void SendSecondaryCollision(NetworkRigidbody hit)Sends another body's state after a collision, on the next frame. Only used when HasSecondaryPhysics is on.
void Serialize(Stream stream, bool isWriteFull)Writes or reads the kinematic flag, position, rotation, sleep state and velocities.
void SetActive(bool isEnabled)Wakes the body's tracking; on a client also starts or stops the client update.
void UpdateRigidbody()Finds the rigidbody on this object again.
protected virtual void OnDisable()Resets the easing strength.
17 private and internal methods
MemberWhat it is
private void ApplySerializedValues(bool isWriteFull)Client: snaps the body to the synced position, kinematic state and velocity when needed, or puts it to sleep if the server's is asleep.
private void Awake()Counts the body in SpawnedCount and AwakeNetworkRigidbodies.
private void CheckClientUpdate(bool isActive)Registers or removes the client fixed-update method when the body becomes active or inactive.
private void ChunkFinishedLoading(Chunk chunk)Marks the spawn chunk as loaded so the client update can put the body to sleep.
private void DelayDestroy()Destroys the entity if it still has no chunk. Used by the obsolete chunk serializer.
private void DisableDestroy(NetworkEntity entity, bool isUnload)Client: on scene destroy, unhooks the chunk load event and the client update.
private IEnumerator FadeDampenJump(float initialLerp, float duration) (iterator)Coroutine that ramps the blend factor from a starting value back up to 0.3 over a duration.
private static bool IsParentNull(NetworkRigidbody target)True when the body has no parent or sits in the spawner's default folder.
private void Alta.Chunks.ISavableFull.LoadSetup()After a load, applies the saved kinematic state and velocity, or puts the body to sleep.
private static void MakeParentNull(NetworkRigidbody target)Unparents the body's transform.
private void OnDestroy()Updates the counters and unhooks the chunk event and client update.
private Vector3 PredictPosition(Vector3 initialVelocity, float time)Works out a position offset from a velocity over time, with gravity. Not called in this build.
private void Alta.Networking.IEntityReplaceHandler.ReplacedWith(NetworkEntity entity, ReplaceAvoidFlags replaceAvoidFlags)When the entity is replaced, copies velocity and angular velocity to the new body unless position is excluded.
private void SecondaryPhysics(IPlayer player, Stream stream, NetworkRigidbody hitNetworkRigidbody)Method sync for a body another body hit: position, rotation and velocities. Server relays it. Only runs if HasSecondaryPhysics.
private IEnumerator SendSecondaryCoroutine(NetworkRigidbody hitRigidbody) (iterator)Waits a physics step and a frame, then sends the secondary collision sync.
private void SerializeChunkAndPosition(Stream stream, ref Chunk chunk, ref Vector3 position)Obsolete: writes or reads a chunk index (13 bits) and the position in that chunk.
private void WaitForChunkToBeLoadedToHandleRemote()Client: finds the chunk the body spawned in and waits for it to fully load if it hasn't.

Used in​

Members of other types that take, return or hold a NetworkRigidbody: