Skip to main content

PathWalker

A class in Root.Township, the global namespace.

Moves a rigidbody along an A* path towards a target transform or position, steering and slowing near the end. Uses the A* Pathfinding Seeker on the same object; nothing else in the code references it.

Constructors​

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

Fields​

MemberWhat it is
bool isMoveConstrainedWhen true the walker stops moving and stops searching for paths.
PathWalker.OnTargetReached OnPathEndReachedDelegate called once when the walker gets within EndReachedDistance of the end.
float speedMaximum walking speed. Defaults to 3.
bool targetReachedTrue once the walker is within the end distance of the target.
Vector2 velocity2DCurrent velocity on the movement plane.
27 private and internal fields
MemberWhat it is
private float accelerationAcceleration towards the path.
private bool canSearchAgainFalse while a path request is waiting for its result.
private float endDistanceBacking field for EndReachedDistance.
private float endDistanceSquaredSquared end distance used for the reached checks.
private LayerMask groundMaskLayers the ground raycast hits.
private PathInterpolator interpolatorTracks the walker's position along the current path.
private float lastRepathTime of the last path request.
private float minimunDistanceUnused minimum distance setting.
private float minTurnSmallest share of sideways acceleration kept when turning.
private MonoModifier monoModifierPath modifier on the same object, enabled with the walker.
private IMovementPlane movementPlaneMovement plane of the graph the path is on.
private Path pathThe current path from the Seeker.
private float pickNextWaypointDistHow far ahead along the path to aim.
private float repathRateSeconds between path searches.
private Rigidbody rigidbodyRigidbody moved with MovePosition.
private float rotationSpeedTurn speed in degrees per second.
private Coroutine searchPathThe running path search coroutine.
private Seeker seekerThe A* Seeker that finds paths.
private float sharpTurnHow sharply the walker turns, from 0 to 1.
private float slowdownDistanceDistance from the end at which the walker starts slowing.
private bool startHasRunTrue once Start has run, so searching can begin.
private Transform targetBacking field for Target.
private Vector3? targetedPositionFixed position to walk to when there is no Target.
private Vector3 targetPointThe point on the path being steered at.
private Transform transformCached transform.
private float TurningPerpendicularRatioHow much sideways acceleration is kept when turning, from sharpTurn and minTurn.
private float verticalVelocityVertical speed, reset when the ground ray hits.

Properties​

MemberWhat it is
float EndReachedDistance { get; set; }How close to the target counts as arrived.
Transform Target { get; set; }Transform to walk to. Setting it starts a new path. Takes priority over TargetPosition.
Vector3 TargetPosition { get; set; }Where the walker is heading: the target's position if there is one, else the set position.

Methods​

MemberWhat it is
void OnDisable()Cancels the current path request and releases the path.
void OnPathComplete(Path _p)Seeker callback. Takes the new path and snaps progress to the closest point. Only handles ABPath.
void StartPath()Drops the current path and starts searching again towards the target.
float TrySearchPath()Requests a new path if the repath interval has passed. Returns the seconds until the next try.
protected void MovementUpdate(float deltaTime)One movement step: steers along the path, fires OnPathEndReached on arrival and moves the rigidbody along the ground.
protected virtual void OnEnable()Hooks up the seeker callback and starts the repeating path search.
protected IEnumerator RepeatTrySearchPath() (iterator)Coroutine that keeps calling TrySearchPath and waits the time it returns.
8 private and internal methods
MemberWhat it is
private void Awake()Disables the component and its modifier, and works out the turning ratio.
private void FixedUpdate()Moves towards the target each physics step until within the end distance.
private void InitializePath()Resets the repath timer and restarts the repeating path search coroutine.
private void Move(Vector3 position3D, Vector3 deltaPosition)Moves the rigidbody by the step, snapping down to the ground.
private Vector3 RaycastPosition(Vector3 position, float lastElevation)Casts down from the previous height to find the ground under the new position.
private void RotateTowards(Vector2 direction, float maxDegrees)Turns the transform towards the movement direction by up to the given degrees.
private void SearchPath()Asks the Seeker for a new path to the target position.
private void Start()Marks start as run and starts path searching.