Skip to main content

DirectionConstrainedRandomMovement

A class in Root.Township, the global namespace.

Moves an object back and forth along one local direction to random points, picked by the server and synced to clients (for example bobbing or swaying props).

Constructors​

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

Fields​

14 private and internal fields
MemberWhat it is
private float acelerationHow fast speed ramps up after a new target; 0 means full speed at once.
private Transform cachedTransformCached transform being moved.
private float currentSpeedMultiplierCurrent speed ramp, 0 to 1.
private Vector3 directionLocal direction of travel (normalised).
private AnimationCurve modulatorCurve for how far each step goes, as a share of the amplitude.
private float movementAmplitudeLength of the line the object moves along.
private float movementSpeedMovement speed, or lerp rate when lerping.
private bool moveWithLerpWhether to ease with lerp instead of moving at a steady speed.
private Vector3 movingTowardsCurrent target local position.
private float nextSignWhich way the next step goes; flips each time.
private MethodSync syncFullFull position sync.
private MethodSyncStruct<float> syncMovementTarget sync.
private IUnityUpdateMethod updatePositionThe update registration.
private bool usesNegativeValuesOnModulatorWhether the modulator is sampled from -1 to 1 instead of 0 to 1.

Methods​

MemberWhat it is
override void Initialize()Sets up the syncs and registers the server or client update.
override void OnConnected(Player player)Server: sends the current target. It sends to all chunks, not only the joining player.
7 private and internal methods
MemberWhat it is
private void GetNewPosition()Server: picks the next target along the direction, alternating sides, and sends it to clients.
private void OnDestroy()Removes the update registration.
private void OnDrawGizmosSelected()Draws the movement line in the parent's space.
private void ServerUpdatePosition()Server: picks a new target on arrival, then moves.
private void SyncFull(IPlayer player, Stream stream)Syncs current position and target as ratios along the line. Not sent anywhere in this class.
private void SyncMovement(IPlayer player, Stream stream, float movingTowardsRatio)Client: receives a new target ratio and restarts the speed ramp.
private void UpdatePosition()Moves toward the target, by lerp or at a steady speed, with optional speed ramp-up.