A* Pathfinding Project  4.1.22
The A* Pathfinding Project for Unity 3D
RaycastModifier Class Reference

Simplifies a path using raycasting. More...

Detailed Description

Simplifies a path using raycasting.

This modifier will try to remove as many nodes as possible from the path using raycasting (linecasting) to validate the node removal. You can use either graph raycasting or Physics.Raycast. When using graph raycasting, the graph will be traversed and checked for obstacles. When physics raycasting is used, the Unity physics system will be asked if there are any colliders which intersect the line that is currently being checked.

See also
https://docs.unity3d.com/ScriptReference/Physics.html
Pathfinding.IRaycastableGraph

This modifier is primarily intended for grid graphs and layered grid graphs. Though depending on your game it may also be useful for point graphs. However note that point graphs do not have any built-in raycasting so you need to use physics raycasting for that graph.

For navmesh/recast graphs the Pathfinding.FunnelModifier is a much better and faster alternative.

On grid graphs you can combine the FunnelModifier with this modifier by simply attaching both of them to a GameObject with a Seeker. This may or may not give you better results. It will usually follow the border of the graph more closely when they are both used however it more often misses some simplification opportunities. When both modifiers are used then the funnel modifier will run first and simplify the path, and then this modifier will take the output from the funnel modifier and try to simplify that even more.

This modifier has several different quality levels. The highest quality is significantly slower than the lowest quality level (10 times slower is not unusual). So make sure you pick the lowest quality that your game can get away with. You can use the Unity profiler to see if it takes up any significant amount of time. It will show up under the heading "Running Path Modifiers".

See also
Using Modifiers

Public Types

enum  Quality { Low, Medium, High, Highest }
 

Public Member Functions

override void Apply (Path p)
 Called for each path that the Seeker calculates after the calculation has finished. More...
 
- Public Member Functions inherited from MonoModifier
virtual void PreProcess (Path path)
 

Public Attributes

LayerMask mask = -1
 Layer mask used for physics raycasting. More...
 
Quality quality = Quality.Medium
 Higher quality modes will try harder to find a shorter path. More...
 
Vector3 raycastOffset = Vector3.zero
 Offset from the original positions to perform the raycast. More...
 
bool thickRaycast
 Checks around the line between two points, not just the exact line. More...
 
float thickRaycastRadius
 Distance from the ray which will be checked for colliders. More...
 
bool use2DPhysics
 Check for intersections with 2D colliders instead of 3D colliders. More...
 
bool useGraphRaycasting
 Use raycasting on the graphs. More...
 
bool useRaycasting = true
 Use Physics.Raycast to simplify the path. More...
 
- Public Attributes inherited from MonoModifier
Seeker seeker
 

Protected Member Functions

bool ValidateLine (GraphNode n1, GraphNode n2, Vector3 v1, Vector3 v2)
 Check if a straight path between v1 and v2 is valid. More...
 
- Protected Member Functions inherited from MonoModifier
virtual void OnDisable ()
 
virtual void OnEnable ()
 Alerts the Seeker that this modifier exists. More...
 
- Protected Member Functions inherited from VersionedMonoBehaviour
virtual void Awake ()
 
virtual int OnUpgradeSerializedData (int version, bool unityThread)
 Handle serialization backwards compatibility. More...
 

Properties

override int Order [get]
 
- Properties inherited from MonoModifier
abstract int Order [get]
 Modifiers will be executed from lower order to higher order. More...
 
- Properties inherited from IPathModifier
int Order [get]
 

Private Member Functions

List< Vector3 > ApplyDP (Path p, List< Vector3 > points)
 
List< Vector3 > ApplyGreedy (Path p, List< Vector3 > points)
 

Static Private Attributes

static List< Vector3 > buffer = new List<Vector3>()
 
static float [] DPCosts = new float[16]
 
static int [] DPParents = new int[16]
 
static readonly int [] iterationsByQuality = new [] { 1, 2, 1, 3 }
 

Member Enumeration Documentation

◆ Quality

enum Quality
strong
Enumerator
Low 

One iteration using a greedy algorithm.

Medium 

Two iterations using a greedy algorithm.

High 

One iteration using a dynamic programming algorithm.

Highest 

Three iterations using a dynamic programming algorithm.

Member Function Documentation

◆ Apply()

override void Apply ( Path  path)
virtual

Called for each path that the Seeker calculates after the calculation has finished.

Implements MonoModifier.

◆ ApplyDP()

List<Vector3> ApplyDP ( Path  p,
List< Vector3 >  points 
)
private

◆ ApplyGreedy()

List<Vector3> ApplyGreedy ( Path  p,
List< Vector3 >  points 
)
private

◆ ValidateLine()

bool ValidateLine ( GraphNode  n1,
GraphNode  n2,
Vector3  v1,
Vector3  v2 
)
protected

Check if a straight path between v1 and v2 is valid.

If both n1 and n2 are supplied it is assumed that the line goes from the center of n1 to the center of n2 and a more optimized graph linecast may be done.

Member Data Documentation

◆ buffer

List<Vector3> buffer = new List<Vector3>()
staticprivate

◆ DPCosts

float [] DPCosts = new float[16]
staticprivate

◆ DPParents

int [] DPParents = new int[16]
staticprivate

◆ iterationsByQuality

readonly int [] iterationsByQuality = new [] { 1, 2, 1, 3 }
staticprivate

◆ mask

LayerMask mask = -1

Layer mask used for physics raycasting.

All objects with layers that are included in this mask will be treated as obstacles. If you are using a grid graph you usually want this to be the same as the mask in the grid graph's 'Collision Testing' settings.

◆ quality

Higher quality modes will try harder to find a shorter path.

Higher qualities may be significantly slower than low quality.

◆ raycastOffset

Vector3 raycastOffset = Vector3.zero

Offset from the original positions to perform the raycast.

Can be useful to avoid the raycast intersecting the ground or similar things you do not want to it intersect

◆ thickRaycast

bool thickRaycast

Checks around the line between two points, not just the exact line.

Make sure the ground is either too far below or is not inside the mask since otherwise the raycast might always hit the ground.

See also
https://docs.unity3d.com/ScriptReference/Physics.SphereCast.html

◆ thickRaycastRadius

float thickRaycastRadius

Distance from the ray which will be checked for colliders.

◆ use2DPhysics

bool use2DPhysics

Check for intersections with 2D colliders instead of 3D colliders.

Useful for 2D games.

See also
https://docs.unity3d.com/ScriptReference/Physics2D.html

◆ useGraphRaycasting

bool useGraphRaycasting

Use raycasting on the graphs.

Only currently works with GridGraph and NavmeshGraph and RecastGraph.

A* Pro Feature:
This is an A* Pathfinding Project Pro feature only. This function/class/variable might not exist in the Free version of the A* Pathfinding Project or the functionality might be limited
The Pro version can be bought here

◆ useRaycasting

bool useRaycasting = true

Use Physics.Raycast to simplify the path.

Property Documentation

◆ Order

override int Order
get

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