A* Pathfinding Project  4.2.5
The A* Pathfinding Project for Unity 3D
ProceduralGridMover Class Reference

Moves a grid graph to follow a target. More...

Detailed Description

Moves a grid graph to follow a target.

Attach this to some object in the scene and assign the target to e.g the player. Then the graph will follow that object around as it moves.

This is useful if pathfinding is only necessary in a small region around an object (for example the player). It makes it possible to have vast open worlds (maybe procedurally generated) and still be able to use pathfinding on them.

When the graph is moved you may notice an fps drop. If this grows too large you can try a few things:

  • Reduce the updateDistance. This will make the updates smaller but more frequent. This only works to some degree however since an update has an inherent overhead.
  • Reduce the grid size.
  • Turn on multithreading (A* Inspector -> Settings)
  • Disable the #floodFill field. However note the restrictions on when this can be done.
  • Disable Height Testing or Collision Testing in the grid graph. This can give a performance boost since fewer calls to the physics engine need to be done.
  • Avoid using any erosion in the grid graph settings. This is relatively slow.

Make sure you have 'Show Graphs' disabled in the A* inspector since gizmos in the scene view can take some time to update when the graph moves and thus make it seem like this script is slower than it actually is.

See also
Take a look at the example scene called "Procedural" for an example of how to use this script
Note
This class does not support the erosion setting on grid graphs. You can instead try to increase the 'diameter' setting under the Grid Graph Settings -> Collision Testing header to achieve a similar effect. However even if it did support erosion you would most likely not want to use it with this script since erosion would increase the number of nodes that had to be updated when the graph moved by a large amount.
Version
Since 3.6.8 this class can handle graph rotation other options such as isometric angle and aspect ratio.
After 3.6.8 this class can also handle layered grid graphs.

Public Member Functions

void UpdateGraph ()
 Updates the graph asynchronously. More...
 

Public Attributes

Transform target
 Graph will be moved to follow this target. More...
 
float updateDistance = 10
 Graph will be updated if the target is more than this number of nodes from the graph center. More...
 

Properties

bool updatingGraph [get, private set]
 True while the graph is being updated by this script. More...
 

Private Member Functions

Vector3 PointToGraphSpace (Vector3 p)
 Transforms a point from world space to graph space. More...
 
void Start ()
 
void Update ()
 Update is called once per frame. More...
 
IEnumerator UpdateGraphCoroutine ()
 Async method for moving the graph. More...
 

Private Attributes

GridNodeBase [] buffer
 Temporary buffer. More...
 
GridGraph graph
 Grid graph to update. More...
 

Additional Inherited Members

- 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...
 

Member Function Documentation

◆ PointToGraphSpace()

Vector3 PointToGraphSpace ( Vector3  p)
private

Transforms a point from world space to graph space.

In graph space, (0,0,0) is bottom left corner of the graph and one unit along the X and Z axes equals distance between two nodes the Y axis still uses world units

◆ Start()

void Start ( )
private

◆ Update()

void Update ( )
private

Update is called once per frame.

◆ UpdateGraph()

void UpdateGraph ( )

Updates the graph asynchronously.

This will move the graph so that the target's position is the center of the graph. If the graph is already being updated, the call will be ignored.

The image below shows which nodes will be updated when the graph moves. The whole graph is not recalculated each time it is moved, but only those nodes that have to be updated, the rest will keep their old values. The image is a bit simplified but it shows the main idea.

If you want to move the graph synchronously then call

Immediately after you have called this method.

◆ UpdateGraphCoroutine()

IEnumerator UpdateGraphCoroutine ( )
private

Async method for moving the graph.

Member Data Documentation

◆ buffer

GridNodeBase [] buffer
private

Temporary buffer.

◆ graph

GridGraph graph
private

Grid graph to update.

◆ target

Transform target

Graph will be moved to follow this target.

◆ updateDistance

float updateDistance = 10

Graph will be updated if the target is more than this number of nodes from the graph center.

Note that this is in nodes, not world units.

Version
The unit was changed to nodes instead of world units in 3.6.8.

Property Documentation

◆ updatingGraph

bool updatingGraph
getprivate set

True while the graph is being updated by this script.


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