Skip to main content

Walker

A class in Root.Township, Alta.Pathfinding.

Moves a creature's rigidbody along its Navigator path with acceleration, turning, pivoting, gravity and optional RVO avoidance. A component on walking creatures; runs in FixedUpdate.

Constructors​

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

Fields​

MemberWhat it is
static Walker chosenStatic slot for a chosen walker; nothing sets it in this build.
static Walker currentStatic, set to the walker clamping itself to the navmesh during that call.
Rigidbody topicThe rigidbody being moved.
51 private and internal fields
MemberWhat it is
private float accelerationBacking field for Acceleration.
private float angularDragAngular drag applied each step at full power.
private float blockedUntilTime until which IsBlocked is true, set when RVO stops all movement.
private Vector2 cacheForwardLast flat forward direction used for turning.
private Vector3 cacheUpLast up direction used for surface alignment.
private float currentMoveDurationDuration of the current normalized move.
private static int currentPhysicsCallIndexShared counter of which of the 5 spread slots runs this physics step.
private float currentTimeTime the current normalized move started.
private float detatchStrengthHow much speed drops when the walker is away from the navmesh path (when not clamped).
private static float fixedDeltaTimeCached Time.fixedDeltaTime.
private float gravityMultiplierBacking field for GravityMultiplier.
private LayerMask groundBacking field for GroundLayers.
private bool hasRVOTrue when an RVOController is present for local avoidance.
private bool isAlignedWithSurfaceIf true, the walker tilts to match the ground normal.
private bool isClampedToNavMeshIf true, positions are clamped onto the navmesh.
private bool isFinalStretchTrue on the last path segment this step; passed in WalkerState.
private bool isFlyingIf true, the walker isn't grounded and keeps up to about 2m above ground.
private bool isMovementForwardOnlyIf true, speed only counts in the facing direction.
private bool isNormalizedMovingTrue while a NormalizedMove is active.
private bool isPivotingTrue while turning in place on a sharp corner.
private bool isSlowedDownWhenNotMovingForwardBacking field for IsSlowedWhenNotMoving.
private bool isWalkingTrue while moving; going false fires Stopped.
private float lastDeltaPer-step height change used to spread the ground move over the 5 steps.
private float lastPhysicsHeightGround height found by the last raycast.
private Vector3 lastUpGround normal from the last raycast, used for surface alignment.
private float maximumSpeedBacking field for MaximumSpeed.
private float minimumPivotAngleTurn angle above which the walker stops and pivots in place.
private Navigator navigatorThe Navigator on the same object that supplies the path.
private static int nextPhysicsCallIndexNext spread slot to give a new walker.
private NormalizedPath normalizedPathTimed path used by NormalizedTravel.
private int physicsCallIndexThis walker's slot in the 5-step spread, or -1 before it's assigned.
private const int physicsFrameSpreadNumber of physics steps ground checks are spread over: 5.
private float pivotingSpeedLimitSpeed below which pivoting can start.
private float powerPerSecondBacking field for PowerPerSecond.
private bool preventRollIf true, surface alignment doesn't roll the walker sideways.
private float raycastHeightHeight above the walker the ground raycast starts from.
private float reevaluationIntervalUnused in this build.
private float rotatedTurn speed this step, as a fraction of rotationSpeed; passed in WalkerState.
private float rotationSpeedBacking field for RotationSpeed.
private RVOController rvoControllerOptional RVO controller for avoiding other agents.
private float stopPivotingRemaining angle at which a pivot ends.
private float surfaceAlignSpeedDegrees per second the walker tilts toward the ground normal.
private float targetSlowdownBacking field for TargetSlowdown.
private Transform transformTransform of topic, the rigidbody being moved.
private static IUnityUpdateMethod updatePhysicsCallIndexFixed update registration for UpdatePhysicsCallIndex, shared by all walkers.
private float velocityDragHorizontal velocity drag applied each step at full power.
private Vector2 velocityXZCurrent horizontal walking velocity.
private float velocityYStored vertical speed used when the rigidbody is kinematic.
private float wallAvoidanceDistanceHow close a navmesh wall must be to push the walker.
private float wallAvoidanceForceStrength of the push away from walls.
private List<Vector3> wallBufferWall segments near the walker, refreshed on its turn.

Properties​

MemberWhat it is
float Acceleration { get; set; }How fast it speeds up.
float DistanceToGround { get; private set; }Distance from the body to the ground below, from the last ground check.
float GravityMultiplier { get; set; }Gravity multiplier; jump actions swap it while jumping.
LayerMask GroundLayers { get; }Layers counted as ground.
bool HasForwardDefined { get; set; }Set by circling actions when they set the facing. Not read by the walker itself in this build.
bool IsBlocked { get; }True for 0.2 s after RVO avoidance stopped it while it wanted to move.
bool IsGrounded { get; private set; }Whether it was on the ground at the last check.
bool IsOvershooting { get; set; }Lets it keep moving past the target instead of stopping exactly on it. Set by navigation actions.
bool IsPaused { get; set; }Holds it still without dropping the path. Cleared when the path finishes.
bool IsSlowedWhenNotMoving { get; set; }Whether it slows while not facing where it is going.
float MaximumSpeed { get; set; }Top speed.
float Power { get; set; }How much control it has (1 is full). Below 1 it recovers at PowerPerSecond; 0 or less stops it moving itself, as when jumping.
float PowerPerSecond { get; set; }How fast Power recovers toward 1.
float RotationSpeed { get; set; }Turn speed in degrees per second.
float TargetSlowdown { get; set; }How much it slows near the target.
1 private and internal property
MemberWhat it is
private float VelocityY { get; set; }Vertical speed: the rigidbody's when not kinematic, otherwise a stored value.

Events​

MemberWhat it is
event Action StoppedRaised when it stops walking because the path finished.
event Action<WalkerState> UpdatedRaised every physics step with its velocity, forward and turn speed, pivoting, final stretch and grounded state.

Methods​

MemberWhat it is
void ClearBlocked()Clears the blocked timer.
void NormalizedMove(float duration)Makes the next path take a set number of seconds overall instead of moving at normal speed.
void StopNormalizedMove()Goes back to normal speed movement.
16 private and internal methods
MemberWhat it is
private void ApplyGravity()Adds gravity (times gravityMultiplier) to the vertical speed each physics step.
private void Awake()Caches the moved rigidbody's transform, the Navigator and the optional RVOController.
private Vector2 CalculateWallForce(Vector3 position, Vector2 directionToTargetXZ)Works out a sideways push away from nearby navmesh walls, relative to the direction of travel.
private float CallIndexMultiplier()Returns five physics steps' worth of time on this walker's turn in the 5-step spread, else 0, so vertical moves happen every fifth step.
private Vector3 FindGround(Vector3 position, float previousY)On this walker's turn, raycasts for ground and records grounding; on other steps it eases toward the last found height.
private void FixedUpdate()Each physics step: stands, travels or follows a normalized path, applies drag, and fires Updated with the walker state.
private void Move(Vector3 position, Vector3 deltaPosition)Moves the rigidbody by a delta, clamping to the navmesh and ground if set, and scaled by Power.
private void NormalizedTravel()Moves along a timed path so the whole route takes the duration given to NormalizedMove.
private void OnDrawGizmos()Draws lines to the next and following path corners.
private void RotateTowards(float speed, Vector2 targetForward, Vector2 direction, float maxDegrees, out Vector2 forward)Turns the rigidbody toward a direction, handles pivoting in place on sharp turns and aligns to the surface if set.
private void SetupNormalizedMovement()When the path is found, builds the timed path for NormalizedMove and sets the RVO target speed.
private void Stand()Stops horizontal movement, turns toward TargetFacing and applies gravity.
private void Start()Server: snaps the walker to the nearest navmesh point.
private void Travel()Normal path following: accelerates toward the next corner with wall avoidance, slows near the target and uses RVO if present.
private void UpdatePhysicsCallIndex()Advances the shared 0 to 4 counter that decides which walkers do ground checks this physics step.
private void UpdatePower()Recovers Power toward 1 over time and applies velocity and angular drag scaled by it.

Used in​

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