A* Pathfinding Project
4.2.6
The A* Pathfinding Project for Unity 3D
|
Simplifies a path using raycasting. More...
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.
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".
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... | |
virtual void | Reset () |
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 } |
|
strong |
|
virtual |
Called for each path that the Seeker calculates after the calculation has finished.
Implements MonoModifier.
|
private |
|
private |
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.
|
staticprivate |
|
staticprivate |
|
staticprivate |
|
staticprivate |
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 quality = Quality.Medium |
Higher quality modes will try harder to find a shorter path.
Higher qualities may be significantly slower than low quality.
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
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.
float thickRaycastRadius |
Distance from the ray which will be checked for colliders.
bool use2DPhysics |
Check for intersections with 2D colliders instead of 3D colliders.
Useful for 2D games.
bool useGraphRaycasting |
Use raycasting on the graphs.
Only currently works with GridGraph and NavmeshGraph and RecastGraph.
bool useRaycasting = true |
Use Physics.Raycast to simplify the path.
|
get |