Skip to main content

Navigator

A class in Root.Township, Alta.Pathfinding.

Wraps an A* Seeker to plan and follow a path for a creature: it requests paths, tracks the current corners and raises events when a path is found or the target is reached. A component used by creature Directors and walkers.

Constructors​

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

Fields​

11 private and internal fields
MemberWhat it is
private float distanceToNextPointSquaredSquared distance to the next corner, checked when a path starts on its final stretch.
private RichFunnel funnelThe current part of the path being followed, if it is a funnel.
private bool isLastCornerTrue when the funnel reports the next corner is the last one.
private bool isRequestingPathBacking field for IsRequestingPath.
private float lastPathRequestBacking field for LastPathRequest.
private Vector3 lastTargetWhere the navigator is heading; RemainingDistance is measured to it.
private static Logger loggerNLog logger for this class.
private static readonly NNConstraint NearestConstraintNearest-node constraint with no distance limit. Unused in this build.
private RichPath pathThe A* rich path being followed.
private Path preparingPathThe wander search in progress, if any.
private float targetReachedDistanceSquaredBacking field for TargetReachedDistanceSquared.

Properties​

MemberWhat it is
List<Vector3> Corners { get; }The next corners of the current path, refreshed by UpdatePosition.
Vector3? FollowingPoint { get; private set; }The corner after NextPoint, or null if there is none.
Func<Vector3> GetTarget { get; set; }Function that gives the target to re-path to when the path goes stale. Defaults to the last requested target.
bool HasFunnel { get; }Whether there is a current path part to follow.
bool IsFinalStretch { get; }True when on the last corner of the last path part.
bool IsFinished { get; private set; }True when there is no path being followed (stopped, reached, or never started).
bool IsRequestingPath { get; }True while a path request is waiting for the pathfinder.
float LastPathRequest { get; }Time.time of the last navigate or wander request.
Vector3 NextPoint { get; private set; }The next corner to walk towards, refreshed by UpdatePosition.
Vector3 Position { get; private set; }The current position clamped to the path, refreshed by UpdatePosition.
float RemainingDistance { get; }Straight-line distance from here to the last target.
float RemainingDistanceSquared { get; }Squared straight-line distance to the target, or 0 when finished and not requesting.
Seeker Seeker { get; private set; }The A* Seeker on the same object.
Vector3? TargetFacing { get; set; }Optional direction to face once at the target. Set by callers; not used inside this class.
float TargetReachedDistanceSquared { get; }Squared distance at which the target counts as reached, from the last request.

Events​

MemberWhat it is
event Action PathFindFailedRaised when a path request fails or a wander finds no nodes.
event Action PathFindSucceededRaised when a path has been found and started.
event Action TargetReachedRaised when the path finishes, including when stopped or teleported mid-path.

Methods​

MemberWhat it is
bool Clamp(ref Vector3 position)Clamps a position onto the current navmesh node in XZ. Returns false if there is no current path node.
void ContinuePath()Moves on to the next part of the path, finishing (and raising TargetReached) if all parts are done.
void GetWalls(List<Vector3> wallBuffer, float range)Fills the buffer with navmesh wall segments within range of the current path position.
void NavigateTo(Vector3 position, float targetReachedDistance = 0.1)Requests a path to a position. Finishes at once if already within the reached distance.
void ResetPosition()Zeroes the local position and rotation and teleports the navigator there.
void Stop()Cancels any pending request and finishes the current path, raising TargetReached if one was running.
Vector3 TeleportTo(Vector3 position, bool isSnappingY)Drops the current path and finishes at the position. Without Y snapping only X and Z of the target change.
void UpdatePosition()Advances along the current path part, refreshing Position, Corners, NextPoint and FollowingPoint; re-paths if needed.
void Wander(Vector3 near, int searchLength, Vector3 idealDirection, int bestOf, float targetReachedDistance = 0.1)Picks a random reachable point within a search length (optionally the best of several towards a direction) and paths there.
void Wander(Vector3 near, int searchLength, float targetReachedDistance = 0.1)Picks a random reachable point within a search length (optionally the best of several towards a direction) and paths there.
void Wander(int searchLength, float targetReachedDistance = 0.2)Picks a random reachable point within a search length (optionally the best of several towards a direction) and paths there.
protected virtual void Awake()Grabs the Seeker on the same object and sets GetTarget to return the last target.
protected virtual void OnDisable()Stops any path and unhooks from the seeker.
protected virtual void OnEnable()Hooks the seeker's path callback.
8 private and internal methods
MemberWhat it is
private void CancelRequest()Cancels any pending path or wander search and clears its callback.
private void Finish()Marks navigation finished and fires TargetReached, if it wasn't already finished.
private Vector3 GetLastTarget()Default GetTarget: returns the last requested target.
private void OnPathFindFailed(Path path)Fires PathFindFailed.
private void PathFound(Path path)Seeker callback: starts following the path, or fires PathFindFailed on error.
private void StartPath(Path standardPath)Loads a found path into the rich path, sets up the first funnel and fires PathFindSucceeded.
private void Wander(Vector3 near, int searchLength, float targetReachedDistance, OnPathDelegate callback)Picks a random reachable point within a search length (optionally the best of several towards a direction) and paths there.
private void WanderTo(Path path)Wander callback: picks a reachable point from the search and paths to it.

Used in​

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