# Class GridGraph Extends NavGraph, IUpdatableGraph, ITransformedGraph, IRaycastableGraph

Generates a grid of nodes.

The GridGraph does exactly what the name implies, generates nodes in a grid pattern.

Grid graphs suit well to when you already have a grid based world. Features:

You can update the graph during runtime (good for e.g Tower Defence or RTS games)

Throw any scene at it, with minimal configurations you can get a good graph from it.

Supports raycast and the funnel algorithm

Predictable pattern

Can apply penalty and walkability values from a supplied image

Perfect for terrain worlds since it can make areas unwalkable depending on the slope

The **The Snap Size** button snaps the internal size of the graph to exactly contain the current number of nodes, i.e not contain 100.3 nodes but exactly 100 nodes.

This will make the "center" coordinate more accurate.

**Updating the graph during runtime**

Any graph which implements the IUpdatableGraph interface can be updated during runtime.

For grid graphs this is a great feature since you can update only a small part of the grid without causing any lag like a complete rescan would.

If you for example just have instantiated a sphere obstacle in the scene and you want to update the grid where that sphere was instantiated, you can do this:

`AstarPath.active.UpdateGraphs (ob.collider.bounds); `

Where **ob** is the obstacle you just instantiated (a GameObject).

As you can see, the UpdateGraphs function takes a Bounds parameter and it will send an update call to all updateable graphs.

A grid graph will update that area and a small margin around it equal to collision testing diameter/2

### See

Graph Updates during Runtime for more info about updating Graph Types during runtime

**Hexagon graphs**

The graph can be configured to work like a hexagon graph with some simple settings. Since 4.1.x the grid graph has a 'Shape' dropdown. If you set it to 'Hexagonal' the graph will behave as a hexagon graph. Often you may want to rotate the graph +45 or -45 degrees.

Note however that the snapping to the closest node is not exactly as you would expect in a real hexagon graph, but it is close enough that you will likely not notice.

**Configure using code**

`// This holds all graph data`

AstarData data = AstarPath.active.data;

// This creates a Grid Graph

GridGraph gg = data.AddGraph(typeof(GridGraph)) as GridGraph;

// Setup a grid graph with some values

int width = 50;

int depth = 50;

float nodeSize = 1;

gg.center = new Vector3(10, 0, 0);

// Updates internal size from the above values

gg.SetDimensions(width, depth, nodeSize);

// Scans all graphs

AstarPath.active.Scan();

**Tree colliders**

It seems that Unity will only generate tree colliders at runtime when the game is started. For this reason, the grid graph will not pick up tree colliders when outside of play mode but it will pick them up once the game starts. If it still does not pick them up make sure that the trees actually have colliders attached to them and that the tree prefabs are in the correct layer (the layer should be included in the 'Collision Testing' mask).

### See

Pathfinding.GraphCollision for documentation on the 'Height Testing' and 'Collision Testing' sections of the grid graph settings.

## Inner Types

## Public Methods

Calculates the grid connections for a single node.

Calculates the grid connections for a single node.

Calculates the grid connections for a single node.

Calculates the grid connections for a cell as well as its neighbours.

Returns a new transform which transforms graph space to world space.

Returns if **node** is connected to it's neighbour in the specified direction.

Number of nodes in the graph.

Erodes the walkable area.

Erodes the walkable area.

Generates the matrix used for translating nodes from grid coordinates to world coordinates.

Returns the nearest node to a position using the specified NNConstraint.

Returns the nearest node to a position using the specified constraint .

Node in the specified cell.

Calls a delegate with all nodes in the graph.

Get all nodes in a rectangle.

All nodes inside the shape.

All nodes inside the bounding box.

Get all nodes in a rectangle.

A rect that contains all nodes that the bounds could touch.

Transform a point in graph space to world space.

Returns true if a connection between the adjacent nodes **n1** and **n2** is valid.

Returns if there is an obstacle between **from** and **to** on the graph.

Returns if there is an obstacle between **from** and **to** on the graph.

Returns if there is an obstacle between the two nodes on the graph.

Returns if there is an obstacle between **from** and **to** on the graph.

Returns if there is an obstacle between **from** and **to** on the graph.

Returns if there is an obstacle between **from** and **to** on the graph.

Draw gizmos for the graph.

Recalculates single node in the graph.

Moves the nodes in this graph.

Relocate the grid graph using new settings.

Updates unclampedSize from width, depth and nodeSize values.

Changes the grid shape.

Set if connection in the specified direction should be enabled.

Returns if there is an obstacle between **from** and **to** on the graph.

Updates position, walkability and penalty for the node.

## Public Static Methods

Calculates the grid connections for a single node.

## Public Variables

Scaling of the graph along the X axis.

Center point of the grid.

Settings on how to check for walkability and height.

If disabled, will not cut corners on obstacles.

Depth (height) of the grid in nodes.

Erosion of the graph.

Tag to start from when using tags for erosion.

Use tags instead of walkability for erosion.

Determines how the size of each hexagon is set in the inspector.

Angle to use for the isometric projection.

Number of layers in the graph.

The max y coordinate difference between two nodes to enable a connection.

The max slope in degrees for a node to be walkable.

The max y coordinate difference between two nodes to enable a connection.

Costs to neighbour nodes.

Index offset to get neighbour nodes.

Number of neighbours for each node.

All nodes in this graph.

Size of one node in world units.

How much penalty is applied depending on the slope of the terrain.

How much extra to penalize very steep angles.

Use position (y-coordinate) to calculate penalty.

Scale factor for penalty when calculating from position.

Offset for the position when calculating penalty.

Rotation of the grid in degrees.

Additional rules to use when scanning the grid graph.

Show the surface of the graph.

Size of the grid.

Holds settings for using a texture as source for a grid graph.

Determines how the graph transforms graph space to world space.

Size of the grid.

If true, all edge costs will be set to the same value.

This is placed here so generators inheriting from this one can override it and set it to false.

Use jump point search to speed up pathfinding.

Width of the grid in nodes.

## Public Static Variables

In GetNearestForce, determines how far to search after a valid node has been found.

Offsets in the X direction for neighbour nodes.

Offsets in the Z direction for neighbour nodes.

## Public Enums

## Inherited Public Members

Returns the nearest node to a position.

Returns the nearest node to a position using the specified NNConstraint.

Calls a delegate with all nodes in the graph until the delegate returns false.

Index of the graph, used for identification purposes.

Used as an ID of the graph, considered to be unique.

Used in the editor to check if the info screen is open.

Default penalty to apply to all nodes.

Inverse of **matrix**.

A matrix for translating/rotating/scaling the graph.

Name of the graph.

Is the graph open in the editor.

Moves nodes in this graph.

Scan the graph.

Scan the graph.

Use to set both matrix and inverseMatrix at the same time.

## Private/Protected Members

Calculates the width/depth of the graph from unclampedSize and nodeSize.

Clips a line segment in graph space to the graph bounds.

Draw the surface as well as an outline of the grid graph.

Magnitude of the cross product a x b.

Magnitude of the cross product a x b.

Deserializes graph type specific node data.

An old format for serializing settings.

Destroys all nodes in the graph.

Cleans up any unmanaged data that the graph has.

Internal method used for erosion.

Internal method used for erosion.

Internal method used for erosion.

True if the node has any blocked connections.

True if the graph exists, false if it has been destroyed.

Get the connecting node from the node at (x,z) in the specified direction.

All nodes inside the shape or if null, the bounding box.

Which neighbours are going to be used when neighbours=6.

Delegate which creates and returns a single instance of the node type for this graph.

Surface normal for each node.

Function for cleaning up references.

Called after all deserialization has been done for all graphs.

Internal method to scan the graph.

Internal method to scan the graph.

Serializes graph type specific node data.

Internal function to update an area of the graph.

Use heigh raycasting normal for max slope calculation.