Skip to main content

LocomotionController

A class in Root.Township, the global namespace.

Moves and turns a player's rig and syncs it. The owning client is in charge of its own position: it streams it to the server, and the server forwards moves to others or asks the client to move. A component on every player controller.

Constructors​

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

Fields​

MemberWhat it is
static AccumulativeAverageCount prepareForWriteTimerShared profiling timer for preparing the stream data.
static AccumulativeAverageCount serializeTimerShared profiling timer for serializing the stream data.
protected float fixedUpdateLerpTimeHow fast remote copies ease to the streamed position each frame. 1 or more snaps.
protected float moveDurationDefault duration for smooth or faded turns.
protected MethodSyncClass<LocomotionController.TeleportArgument> syncMovementNetwork message carrying a move (position, chunk, teleport mode and function).
protected MethodSyncStruct<Quaternion> syncRotationNetwork message carrying a rotation.
protected LocomotionStyle teleportStyleDefault style (instant, lerp or fade) for turns.
20 private and internal fields
MemberWhat it is
private Vector3 anchorPositionLast position that raised the position events in Update.
private Quaternion anchorRotationLast rotation that raised the rotation events in Update.
private float angleFromAnchorToCallEventDotHow far the rotation must turn from the anchor before Update raises the rotation events, as a dot. About 20 degrees.
private float angleThresholdDotRotation change below this (as a dot) isn't streamed.
private Coroutine currentRotateRunning LerpRotation coroutine, stopped when a new rotation starts.
private float distanceFromAnchorToCallEventSquaredSquared distance the player must move from the anchor before Update raises the position events.
private bool isStreamingBacking field for IsStreaming.
private static Logger loggerNLog logger for this class.
private float movementThresholdSquaredSquared movement below this isn't streamed.
private PlayerController playerControllerBacking field for PlayerController.
private PositionSerializer positionSerializerHolds and serializes the streamed position and rotation, and the movement context.
private float snapDistanceSquaredSquared distance at which a client snaps a remote player to the streamed position and logs a warning instead of lerping. Defaults to 2 m.
private LocomotionStatSettings statSettingsWhich stats drive the speed and cripple modifiers.
private MethodSyncStruct<Vector3> syncFaceTowardsXZMethod sync for SyncFaceRequest.
private MethodSyncStruct<PositionAndRotationTarget> syncMoveToAndFaceTowardsXZMethod sync for SyncMoveToAndFaceRequest.
private Transform targetTransformThe transform that's moved and rotated, cached from this object.
private const int UnchangedResendConstant 30. Not used by this class.
private AnimationCurve weightSpeedMultiplierCurve from the weight stat's value to a speed multiplier.
private Stat weightStatThe player's weight stat, looked up on first use.
private StatDefinition weightStatDefinitionStat definition for carried weight. When unset, weight doesn't slow the player.

Properties​

MemberWhat it is
float CrippledStatSpeedModifier { get; private set; }Speed factor from the cripple stat, kept up to date for the local player.
float EnvironmentSpeedModifier { get; set; }Speed factor set by the surroundings. Default 1.
bool IsGrounded { get; set; }Whether the player is on the ground. Set by the movement code; default true.
bool IsStreaming { get; set; }Whether position and rotation are streamed every network update. Setting it also enables or disables the component.
float LastSyncTime { get; private set; }Level time of the last streamed position update. 0 until the first one.
Quaternion LatestRotation { get; private set; }The change in rotation from the last RotateTo call.
IMovementContext MovementContext { get; set; }Movement context of the position serializer, which positions are relative to.
SoundEffect OverrideStepSound { get; set; }Footstep sound to use instead of the surface's. None means no override.
PlayerController PlayerController { get; }The player controller on the same object.
float SpeedStatModifier { get; private set; }Speed factor from the speed stat, kept up to date for the local player.
float TeleportSpeedMultiplier { get; }Product of the environment, speed stat, cripple and weight factors.
float WeightSpeedModifier { get; }Speed factor from carried weight, from a curve over the weight stat. 1 if there is no weight stat.
3 private and internal properties
MemberWhat it is
private int Alta.Networking.INetworkSerialized.MaximumBits { get; }Explicit INetworkSerialized property: at most 99 bits per write.
private AccumulativeAverageCount Alta.Networking.INetworkSerialized.PrepareForWriteTimer { get; }Explicit INetworkSerialized property: returns the static prepareForWriteTimer.
private AccumulativeAverageCount Alta.Networking.INetworkSerialized.SerializeTimer { get; }Explicit INetworkSerialized property: returns the static serializeTimer.

Events​

MemberWhat it is
event Action<Vector3> DistanceTravelledRaised with the movement vector for teleport, reposition and smooth moves (smooth moves scaled by a setting).
event PositionChangedHandler PositionAboutToChangeRaised just before the rig moves, with old and new positions and the LocomotionFunction.
event PositionChangedHandler PositionChangedRaised after the rig moves, or drifts past a threshold. Fires often; the server's chunk tracking listens to it.
event Action<Vector3, Quaternion> RelativeRotationRaised with the player position and the rotation change when the rig turns.
event RotationChangedHandler RotationChangedRaised with the old and new rotation when the rig turns.
event Action<bool> StreamingSetRaised when IsStreaming changes.

Methods​

MemberWhat it is
void FaceTowardsXZ(Vector3 forward)Turns the player to face a horizontal direction. From the server it asks the owning client to do it.
override void Initialize()Sets up the movement, rotation and face-towards network messages and the position serializer.
virtual void MoveTo(Vector3 position, LocomotionFunction function)Moves the player's feet to a position. The local player moves and syncs; the server only sends Reposition moves to the owner.
void MoveTo(Vector3 position, TeleportMode mode, LocomotionFunction function)Moves the player's feet to a position. The local player moves and syncs; the server only sends Reposition moves to the owner.
void MoveToAndFace(Vector3 position, Vector3 forward)Moves the player and turns them to face a direction. From the server it asks the owning client.
void RotateAroundPlayer(Quaternion rotation)Turns the rig while keeping the player's real feet in the same spot.
virtual void RotateAroundPlayer(Quaternion rotation, LocomotionStyle teleportStyle, float duration)Turns the rig while keeping the player's real feet in the same spot.
void RotateTo(Quaternion rotation)Turns the rig, instantly, smoothly or with a fade. The local player syncs the new rotation.
void RotateTo(Quaternion rotation, LocomotionStyle style, float duration = 0.02)Turns the rig, instantly, smoothly or with a fade. The local player syncs the new rotation.
void ServerForcePosition(Vector3 position)Server: sets the position the server holds and marks it for sync. On its own the client will move it back.
void Translate(Vector3 translation)Shifts the remote target position by an offset.
protected Vector3 GetTeleportTarget(Vector3 position, TeleportMode mode)Converts a target position for a TeleportMode (feet, head or room centre) into the rig position.
protected virtual void InternalFaceTowardsXZ(Vector3 forward)Sets the rotation to look along the given direction. Subclasses override it.
protected virtual void OnEnable()Resets the anchor position and rotation used for the change events.
protected virtual void Update()On other players' copies, eases towards the streamed position. Also raises the change events when the rig drifts past the anchor thresholds.
16 private and internal methods
MemberWhat it is
private bool DisableOnApply()Inspector helper that disables the component when teleportStyle is applied.
private void HandleRemote()Client, for other players: lerps the transform toward the last streamed position and rotation.
private void IInitializeAsLocalPlayer.InitializeAsLocalPlayer(PlayerController player)Explicit IInitializeAsLocalPlayer method: follows the speed and cripple stats into SpeedStatModifier and CrippledStatSpeedModifier.
private void InternalMoveToAndFaceTowardsXZ(Vector3 position, Vector3 forward)Repositions the player and then turns them to face a direction.
private IEnumerator LerpRotation(Quaternion rotation, float duration) (iterator)Coroutine that smooth-damps the rotation to a target over a duration.
private void OnDrawGizmos()Draws a sphere at the streamed live position while playing.
private void OnPositionAboutToChange(Vector3 oldPosition, Vector3 newPosition, LocomotionFunction function)Fires PositionAboutToChange, logging any exception.
private void OnPositionChanged(Vector3 oldPosition, Vector3 newPosition, LocomotionFunction function)Fires PositionChanged, and DistanceTravelled for teleports, repositions and smooth movement.
private void OnRotationChanged(Quaternion oldRotation, Quaternion newRotation)Fires RotationChanged and RelativeRotation, logging any exception.
private bool Alta.Networking.INetworkSerialized.PrepareForWrite(bool isWriteFull)Explicit INetworkSerialized method: whether there's movement to stream, skipping changes under the movement and angle thresholds.
private void Alta.Networking.INetworkSerialized.Serialize(Stream stream, bool isWriteFull)Explicit INetworkSerialized method: streams position and rotation; the server and far-off clients snap to it.
private bool ShouldSendSync(LocomotionFunction function)Whether a move should go as its own message: always when not streaming, otherwise only for reposition and recovery.
private void SyncFaceRequest(IPlayer player, Stream stream, Vector3 forward)Server to the owning client: turns the local player to face a direction.
private void SyncMovement(IPlayer player, Stream stream, LocomotionController.TeleportArgument arguments)Syncs a teleport or move. The server only accepts it from the owning player, applies it, counts distance travelled and relays it.
private void SyncMoveToAndFaceRequest(IPlayer player, Stream stream, PositionAndRotationTarget positionAndRotationTarget)Server to the owning client: moves the local player and turns them.
private void SyncRotation(IPlayer player, Stream stream, Quaternion toRotation)Syncs a rotation. The server only accepts it from the owning player, then relays it.

Used in​

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