PathBerserker2d  1.0
Platformer pathfinding solution
Public Types | Public Member Functions | Public Attributes | Properties | Events | List of all members
PathBerserker2d.NavAgent Class Reference

Represents a pathfinding entity. More...

Inheritance diagram for PathBerserker2d.NavAgent:

Public Types

enum  State { State.Idle, State.FollowPath }
 

Public Member Functions

delegate void FailedToFindPathDelegate (NavAgent agent)
 
bool WarpToNearestSegment ()
 Repositions the agent at the nearest segment the agent could be standing at. Segments the agent could not be at do to its tag or slope will be ignored. More...
 
bool PathTo (params Vector2[] goals)
 Starts the process of pathfinding to the closest of the given goals. NOTE: Do not call this method every frame. Calculating a path takes longer than a frame, so the agent will never start moving. More...
 
bool PathTo (Vector2 goal)
 Starts the process of pathfinding to the given goal. NOTE: Do not call this method every frame. Calculating a path takes longer than a frame, so the agent will never start moving. More...
 
bool UpdatePath (params Vector2[] goals)
 Starts the process of pathfinding to the closest of the given goals. Will continue moving until the calculations for the new path are completed. NOTE: Do not call this method every frame. Calculating a path takes longer than a frame, so the agent will never start moving. More...
 
bool UpdatePath (Vector2 goal)
 Starts the process of pathfinding to the given goal. Will continue moving until the calculations for the new path are completed. NOTE: Do not call this method every frame. Calculating a path takes longer than a frame, so the agent will never start moving. More...
 
bool HasReachedCurrentSubGoal (float maxDist=0.05f)
 Simple distance check between agent and CurrentSubGoal. More...
 
bool SetRandomDestination ()
 Start pathfinding to a random position on the NavGraph. It cannot grantee that this position is reachable. Does the agent might not move after this is called. More...
 
void CompleteLinkTraversal ()
 If you implement link traversal yourself, call this to complete a link traversal. More...
 
void CompleteSegmentTraversal ()
 If you implement segment traversal yourself, call this to complete a segment traversal. More...
 
bool CanTraverseLink (INavLinkInstance link)
 Determines, if in this agent is allowed to traverse the given link. More...
 
float GetLinkTraversalMultiplier (int linkType)
 Get the traversal cost multiplier for a given link type. More...
 
float GetNavTagTraversalMultiplier (int navTag)
 Get the traversal cost multiplier for a given nav tag. More...
 
bool IsOnSegmentWithTag (int navTag)
 Whether the agents current position contains the given NavTag. NOTE: Does not work, if the agent is not currently moving on a path. More...
 
void Stop ()
 Stops the current path following at the first opportunity. Link traversal will be completed before the agent stops. More...
 
void ForceStop ()
 Stops the current path following instantly. Agent might stop wihle traversing a link (e.g. while jumping in mid air) More...
 
bool? IsOnSameSegmentAs (Vector2 other)
 Tries to map the "other" and checks if the agent is mapped to the same segment. If "other" can't be mapped this will return null. Agents on a link will always return false. If this agent currently can't be mapped this will return null. More...
 
IEnumerable< Vector2 > PathPoints ()
 Enumerates the points on the currently followed path. Corner links will result in the same point being enumerated twice in a row. First point will be the agents current position. More...
 

Public Attributes

State CurrentStatus => status
 
bool IsIdle => status == State.Idle && currentPathRequest.Status == PathRequest.RequestState.Draft
 
bool IsFollowingAPath => status == State.FollowPath
 
float Height => height
 
float MaxSlopeAngle => maxSlopeAngle
 
bool IsOnLink => IsFollowingAPath && movementState == MovementState.OnLink
 
bool IsMovingOnSegment => IsFollowingAPath && movementState == MovementState.OnSegment
 
bool IsWaitingForLink => IsFollowingAPath && movementState == MovementState.WaitForLinkOnSegment
 
bool IsOnSegment => IsFollowingAPath && movementState != MovementState.OnLink
 If true, either IsMovingOnSegment is true, or the agent is waiting to traverse an untraversable link. More...
 
bool HasValidPosition => !currentMappedPosition.IsInvalid()
 Check, if the current mapped position of the agent is valid. The mapped position can only be valid, if the agent is close to the ground. (Close, not necessarily directly on the ground) More...
 
bool IsStopping => stopRequested
 True, if Stop() was called and agent hasn't yet stopped More...
 
bool IsOnGoalSegment => IsOnSegment && !Path.HasNext
 True, if the agent is on the last segment of its path. More...
 
INavLinkInstance CurrentLink => currentPath?.CurrentSegment.link ?? null
 Link of the path segment the agent is on, or null. More...
 
string CurrentLinkType => currentPath?.CurrentSegment.link?.LinkTypeName ?? ""
 Link type of the path segment the agent is on, or "" More...
 
Vector2 CurrentLinkStart => currentPath?.CurrentSegment.LinkStart ?? Vector2.zero
 Link start of the path segment the agent is on, or Vector2.zero More...
 
Vector2 CurrentSegmentNormal => IsFollowingAPath ? currentPath.CurrentSegment.SegmentNormal : (currentMappedPosition.cluster?.Normal ?? Vector2.up)
 Segment normal of the path segment the agent is on, or Vector2.up More...
 
Vector2 NextSegmentNormal => currentPath?.NextSegment?.SegmentNormal ?? Vector2.zero
 Segment normal of the next segment on the path, or Vector2.zero More...
 
Vector2 PathGoal => currentPath?.Goal ?? Vector2.zero
 Overall goal of the path the agent is on More...
 
int CurrentNavTagVector => IsFollowingAPath ? currentPath.CurrentSegment.GetTagVector(Position) : (currentMappedPosition.cluster?.GetNavTagVector(currentMappedPosition.t) ?? 0)
 Combination of all NavTags at the position of the agent. More...
 
float movementSpeed = 5
 Obsolete More...
 
float cornerSpeed = 100
 Obsolete More...
 
float jumpSpeed = 5
 Obsolete More...
 
float fallSpeed = 5
 Obsolete More...
 
float climbSpeed = 5
 Obsolete More...
 

Properties

Vector2 PathSubGoal [get]
 Current subgoal the agent is moving towards. May either be a link start or a link end. If it lies on a moving platform, the value may change from frame to frame. More...
 
Vector2 Position [get, set]
 Shorthand for transform.position More...
 
float TimeOnLink [get]
 Time agent spend on link. Does not include waiting for link to become traversable. More...
 

Events

Action< NavAgentOnStartLinkTraversal
 Fired when agent begins moving on a link. More...
 
Action< NavAgentOnLinkTraversal
 Fired when agent is moving on a link. More...
 
Action< NavAgentOnStartSegmentTraversal
 Fired when agent start moving on a segment. More...
 
Action< NavAgentOnSegmentTraversal
 Fired when agent is moving on a link. More...
 
Action< NavAgentOnFailedToFindPath
 Fired when the agent fails to find a path More...
 
Action< NavAgentOnStop
 Fired when agent stops after Stop() or ForceStop() was called. For ForceStop() this happens instantly. For Stop() this happens after the agent stopped. More...
 
Action< NavAgentOnReachedGoal
 Fired when agent reaches its current goal. More...
 
Action< NavAgentOnStartFollowingNewPath
 Called when the agent starts following a new path. NOTE: Also called, after successfully recalculating a path, even if the path itself does not change. More...
 

Detailed Description

Represents a pathfinding entity.

This components handles the interaction with the asynchronous pathfinding system. It assumes the agent is a point located at transform.position. Automatic movement will directly modify the transform this script is attached to. See NavAgent's build-in movement for more detail on movement.

States

At heart the NavAgent is a state machine with the following states:

Pathfinding properties

A NavAgent has a few properties relevant to the pathfinder.

Member Enumeration Documentation

◆ State

Enumerator
Idle 

Agent is doing nothing.

FollowPath 

Agent is following a path.

Member Function Documentation

◆ CanTraverseLink()

bool PathBerserker2d.NavAgent.CanTraverseLink ( INavLinkInstance  link)

Determines, if in this agent is allowed to traverse the given link.

◆ CompleteLinkTraversal()

void PathBerserker2d.NavAgent.CompleteLinkTraversal ( )

If you implement link traversal yourself, call this to complete a link traversal.

◆ CompleteSegmentTraversal()

void PathBerserker2d.NavAgent.CompleteSegmentTraversal ( )

If you implement segment traversal yourself, call this to complete a segment traversal.

◆ ForceStop()

void PathBerserker2d.NavAgent.ForceStop ( )

Stops the current path following instantly. Agent might stop wihle traversing a link (e.g. while jumping in mid air)

◆ GetLinkTraversalMultiplier()

float PathBerserker2d.NavAgent.GetLinkTraversalMultiplier ( int  linkType)

Get the traversal cost multiplier for a given link type.

◆ GetNavTagTraversalMultiplier()

float PathBerserker2d.NavAgent.GetNavTagTraversalMultiplier ( int  navTag)

Get the traversal cost multiplier for a given nav tag.

◆ HasReachedCurrentSubGoal()

bool PathBerserker2d.NavAgent.HasReachedCurrentSubGoal ( float  maxDist = 0.05f)

Simple distance check between agent and CurrentSubGoal.

Returns
True, if distance is less than maxDist

◆ IsOnSameSegmentAs()

bool? PathBerserker2d.NavAgent.IsOnSameSegmentAs ( Vector2  other)

Tries to map the "other" and checks if the agent is mapped to the same segment. If "other" can't be mapped this will return null. Agents on a link will always return false. If this agent currently can't be mapped this will return null.

Parameters
other

◆ IsOnSegmentWithTag()

bool PathBerserker2d.NavAgent.IsOnSegmentWithTag ( int  navTag)

Whether the agents current position contains the given NavTag. NOTE: Does not work, if the agent is not currently moving on a path.

Returns
True, if current position has supplied NavTag.

◆ PathPoints()

IEnumerable<Vector2> PathBerserker2d.NavAgent.PathPoints ( )

Enumerates the points on the currently followed path. Corner links will result in the same point being enumerated twice in a row. First point will be the agents current position.

◆ PathTo() [1/2]

bool PathBerserker2d.NavAgent.PathTo ( params Vector2[]  goals)

Starts the process of pathfinding to the closest of the given goals. NOTE: Do not call this method every frame. Calculating a path takes longer than a frame, so the agent will never start moving.

See also
UpdatePath(Vector2[])
Parameters
goalsGoals to pathfind to.
Returns
True, if the at least 1 goal and the agents own position could be mapped. This does not mean, that a path towards a goal exists.

◆ PathTo() [2/2]

bool PathBerserker2d.NavAgent.PathTo ( Vector2  goal)

Starts the process of pathfinding to the given goal. NOTE: Do not call this method every frame. Calculating a path takes longer than a frame, so the agent will never start moving.

See also
UpdatePath(Vector2)
Parameters
goalsGoals to pathfind to.
Returns
True, if the at least 1 goal and the agents own position could be mapped. This does not mean, that a path towards a goal exists.

◆ SetRandomDestination()

bool PathBerserker2d.NavAgent.SetRandomDestination ( )

Start pathfinding to a random position on the NavGraph. It cannot grantee that this position is reachable. Does the agent might not move after this is called.

Returns

◆ Stop()

void PathBerserker2d.NavAgent.Stop ( )

Stops the current path following at the first opportunity. Link traversal will be completed before the agent stops.

◆ UpdatePath() [1/2]

bool PathBerserker2d.NavAgent.UpdatePath ( params Vector2[]  goals)

Starts the process of pathfinding to the closest of the given goals. Will continue moving until the calculations for the new path are completed. NOTE: Do not call this method every frame. Calculating a path takes longer than a frame, so the agent will never start moving.

See also
PathTo(Vector2[])
Parameters
goalsGoals to pathfind to.
Returns
True, if the at least 1 goal and the agents own position could be mapped. This does not mean, that a path towards a goal exists.

◆ UpdatePath() [2/2]

bool PathBerserker2d.NavAgent.UpdatePath ( Vector2  goal)

Starts the process of pathfinding to the given goal. Will continue moving until the calculations for the new path are completed. NOTE: Do not call this method every frame. Calculating a path takes longer than a frame, so the agent will never start moving.

See also
PathTo(Vector2)
Parameters
goalsGoals to pathfind to.
Returns
True, if the at least 1 goal and the agents own position could be mapped. This does not mean, that a path towards a goal exists.

◆ WarpToNearestSegment()

bool PathBerserker2d.NavAgent.WarpToNearestSegment ( )

Repositions the agent at the nearest segment the agent could be standing at. Segments the agent could not be at do to its tag or slope will be ignored.

Returns
True, if warping was successful

Member Data Documentation

◆ climbSpeed

float PathBerserker2d.NavAgent.climbSpeed = 5

Obsolete

◆ cornerSpeed

float PathBerserker2d.NavAgent.cornerSpeed = 100

Obsolete

◆ CurrentLink

INavLinkInstance PathBerserker2d.NavAgent.CurrentLink => currentPath?.CurrentSegment.link ?? null

Link of the path segment the agent is on, or null.

◆ CurrentLinkStart

Vector2 PathBerserker2d.NavAgent.CurrentLinkStart => currentPath?.CurrentSegment.LinkStart ?? Vector2.zero

Link start of the path segment the agent is on, or Vector2.zero

◆ CurrentLinkType

string PathBerserker2d.NavAgent.CurrentLinkType => currentPath?.CurrentSegment.link?.LinkTypeName ?? ""

Link type of the path segment the agent is on, or ""

◆ CurrentNavTagVector

int PathBerserker2d.NavAgent.CurrentNavTagVector => IsFollowingAPath ? currentPath.CurrentSegment.GetTagVector(Position) : (currentMappedPosition.cluster?.GetNavTagVector(currentMappedPosition.t) ?? 0)

Combination of all NavTags at the position of the agent.

◆ CurrentSegmentNormal

Vector2 PathBerserker2d.NavAgent.CurrentSegmentNormal => IsFollowingAPath ? currentPath.CurrentSegment.SegmentNormal : (currentMappedPosition.cluster?.Normal ?? Vector2.up)

Segment normal of the path segment the agent is on, or Vector2.up

◆ fallSpeed

float PathBerserker2d.NavAgent.fallSpeed = 5

Obsolete

◆ HasValidPosition

bool PathBerserker2d.NavAgent.HasValidPosition => !currentMappedPosition.IsInvalid()

Check, if the current mapped position of the agent is valid. The mapped position can only be valid, if the agent is close to the ground. (Close, not necessarily directly on the ground)

◆ IsOnGoalSegment

bool PathBerserker2d.NavAgent.IsOnGoalSegment => IsOnSegment && !Path.HasNext

True, if the agent is on the last segment of its path.

◆ IsOnSegment

bool PathBerserker2d.NavAgent.IsOnSegment => IsFollowingAPath && movementState != MovementState.OnLink

If true, either IsMovingOnSegment is true, or the agent is waiting to traverse an untraversable link.

◆ IsStopping

bool PathBerserker2d.NavAgent.IsStopping => stopRequested

True, if Stop() was called and agent hasn't yet stopped

◆ jumpSpeed

float PathBerserker2d.NavAgent.jumpSpeed = 5

Obsolete

◆ movementSpeed

float PathBerserker2d.NavAgent.movementSpeed = 5

Obsolete

◆ NextSegmentNormal

Vector2 PathBerserker2d.NavAgent.NextSegmentNormal => currentPath?.NextSegment?.SegmentNormal ?? Vector2.zero

Segment normal of the next segment on the path, or Vector2.zero

◆ PathGoal

Vector2 PathBerserker2d.NavAgent.PathGoal => currentPath?.Goal ?? Vector2.zero

Overall goal of the path the agent is on

Property Documentation

◆ PathSubGoal

Vector2 PathBerserker2d.NavAgent.PathSubGoal
get

Current subgoal the agent is moving towards. May either be a link start or a link end. If it lies on a moving platform, the value may change from frame to frame.

◆ Position

Vector2 PathBerserker2d.NavAgent.Position
getset

Shorthand for transform.position

◆ TimeOnLink

float PathBerserker2d.NavAgent.TimeOnLink
get

Time agent spend on link. Does not include waiting for link to become traversable.

Event Documentation

◆ OnFailedToFindPath

Action<NavAgent> PathBerserker2d.NavAgent.OnFailedToFindPath

Fired when the agent fails to find a path

◆ OnLinkTraversal

Action<NavAgent> PathBerserker2d.NavAgent.OnLinkTraversal

Fired when agent is moving on a link.

◆ OnReachedGoal

Action<NavAgent> PathBerserker2d.NavAgent.OnReachedGoal

Fired when agent reaches its current goal.

◆ OnSegmentTraversal

Action<NavAgent> PathBerserker2d.NavAgent.OnSegmentTraversal

Fired when agent is moving on a link.

◆ OnStartFollowingNewPath

Action<NavAgent> PathBerserker2d.NavAgent.OnStartFollowingNewPath

Called when the agent starts following a new path. NOTE: Also called, after successfully recalculating a path, even if the path itself does not change.

◆ OnStartLinkTraversal

Action<NavAgent> PathBerserker2d.NavAgent.OnStartLinkTraversal

Fired when agent begins moving on a link.

◆ OnStartSegmentTraversal

Action<NavAgent> PathBerserker2d.NavAgent.OnStartSegmentTraversal

Fired when agent start moving on a segment.

◆ OnStop

Action<NavAgent> PathBerserker2d.NavAgent.OnStop

Fired when agent stops after Stop() or ForceStop() was called. For ForceStop() this happens instantly. For Stop() this happens after the agent stopped.


The documentation for this class was generated from the following file: