Skip to main content

GridExtended

This class is used to access grid utility methods like finding neighbours. It auto-detects the grid layout (e.g. rectangular or hexagonal) upon initializing TilemapExtended and knows the correct methods for each layout. This ensures that you don't have to worry about setting or switching them manually somewhere.

tip

All methods returning cells will check if a cell exists at all found positions. For example, if in a hex grid only four of six neighbour cells exist, only four will be returned when calling GetNeighbours(). If you are interested in doing operations at all neighbour grid positions consider using those alternative methods that return all positions.

Public methods

CheckLineOfSight

public bool CheckLineOfSight(Vector3Int startPoint, Vector3Int endPoint)

Checks if the given end point is in sight from the given start position. Returns false if the end point cannot be seen because of obstacles or if it doesn't exist.

Parameters

  • startPoint: The starting point from which to look.
  • endPoint: The position to be checked if in view from the given start point.

Example

var playerPosition = new Vector3Int(0, 0, 0);
var target = new Vector3Int(5, 2, 0);
//Checks if target position can be seen from player position
var inSight = tilemapExtended.GridExtended.CheckLineOfSight(playerPosition, target);

if (inSight) { /*...*/ }

GetCellCenterWorld

public Vector3 GetCellCenterWorld(Vector3Int position)

Returns the given cell center in world coordinates.

note

This method is the same as calling Unity's Grid.GetCellCenterWorld and only exists to simplify the workflow.

Parameters

  • position: The grid position.

Example

var position = new Vector3Int(0, 0, 0);
//Retrieves the cell center in world coords
var worldPos = tilemapExtended.GridExtended.GetCellCenterWorld(position);

SpawnUnit(worldPosition);

GetCellWorld

public Vector3 GetCellWorld(Vector3Int position)

Returns the given cell location in world coordinates.

note

This method is the same as calling Unity's Grid.CellToWorld and only exists to simplify the workflow.

Parameters

  • position: The grid position.

GetDistance

public int GetDistance(Vector3Int start, Vector3Int destination)

Returns the distance in the grid between two grid positions.

Parameters

  • start: The starting grid position.
  • destination: The target grid position.

Example

var player = new Vector3Int(0, 0, 0);
var target = new VectorInt(4, 5, 0);

//Gets the grid distance between the player and a target
var distance = tilemapExtended.GridExtended.GetDistance(player, target);

distance Visualizes the grid distances from the center (red dot).


GetEuclideanDistance

public float GetEuclideanDistance(Vector3Int start, Vector3Int destination)

Returns the euclidean distance between two grid positions. This method is useful when more exact distances are needed (for example in pathfinding). Keep in mind, that this method is computationally more expensive than GetDistance().

Parameters

  • start: The starting grid position.
  • destination: The target grid position.

euclidDistance Visualizes the euclidean distances from the center (red dot).


GetLine

public List<CellData> GetLine(Vector3Int start, Vector3Int destination)

Returns a list of cells under a direct line between two grid positions. The line includes both start and end points.

Parameters

  • start: The starting grid position.
  • destination: The target grid position.

Example

var player = new Vector3Int(0, 0, 0);
var target = new VectorInt(4, 2, 0);

//Gets a line between the player and a target
var line = tilemapExtended.GridExtended.GetLine(player, target);

foreach (var point in line) {
//Do something with points in line
}

line Blue dots are cells at the nearest positions returned on a direct line (orange) between start (red dot) and destination (green dot).


GetLinePositions

public List<Vector3Int> GetLinePositions(Vector3Int start, Vector3Int destination)

Returns a list of grid positions under a direct line between two grid positions. The line includes both start and end points. This method is useful if you want to do operations at all neighbour positions and don't care if cells exists or not.

Parameters

  • start: The starting grid position.
  • destination: The target grid position.

GetNeighbours

public List<CellData> GetNeighbours(Vector3Int position)

Returns a list containing the neighbour cells of the given grid position.

tip

If you also want the diagonal neighbours in a rectangular grid, use GetNeighboursInRadius with a radius of 1 instead.

Parameters

  • position: The grid position the fetch the neighbours from.

Example

var player = new Vector3Int(0, 0, 0);

//Gets the neighbours of the player position
var neighbours = tilemapExtended.GridExtended.GetNeighbours(player);

foreach (var neighbour in neighbours) {
//Do something with neighbours
}

neighbours Blue dots are the neighbours of the center position (red dot).


GetNeighboursInRadius

public List<CellData> GetNeighboursInRadius(Vector3Int centerPosition, int radius)

Returns a list containing all neighbour cells of the given grid position within a given radius. This includes diagonals in a rectangular grid.

Parameters

  • centerPosition: The grid position the fetch the neighbours from.
  • radius: The radius of the circle from the center.

Example

var player = new Vector3Int(0, 0, 0);

//Gets all surrounding cells around the player with a grid distance of 2
var neighbours = tilemapExtended.GridExtended.GetNeighboursInRadius(player, 2);

foreach (var neighbour in neighbours) {
//Do something with neighbours
}

neighbours Blue dots are all neighbours of the center position (red dot) with a radius of 2.


GetNeighbourPositions

public Vector3Int[] GetNeighbourPositions(Vector3Int position)

Returns an array containing the neighbour positions of the given grid position. This method is useful if you want to do operations at all neighbour positions and don't care if cells exists or not.

tip

If you also want the diagonal neighbours in a rectangular grid, use GetNeighbourPositionsInRadius() with a radius of 1 instead.

Parameters

  • position: The grid position the fetch the neighbours from.

GetNeighbourPositionsInRadius

public List<Vector3Int> GetNeighbourPositionsInRadius(Vector3Int centerPosition, int radius)

Returns a list containing the neighbour positions of the given grid position within a given radius. This method is useful if you want to do operations at all neighbour positions and don't care if cells exists or not.

Parameters

  • centerPosition: The grid position the fetch the neighbours from.
  • radius: The radius of the circle from the center.

GetNearestCellPosition

public Vector3Int GetNearestCellPosition(Vector3 position)

Returns the nearest grid position of a given world coordinate.

note

This method is the same as calling Unity's Grid.WorldToCell and only exists to simplify the workflow.

Parameters

  • position: The position in world coordinates.

GetRing

public List<CellData> GetRing(Vector3Int centerPosition, int radius)

Returns a list of cells on a ring around a given center with a given radius.

Parameters

  • centerPosition: The center position of the ring.
  • radius: The radius of the ring from the center position.

Example

var center = new Vector3Int(0, 0, 0);
//Gets cells on a ring with a distance of 2 inbetween
var ring = tilemapExtended.GridExtended.GetRing(position, 2);

ring Blue dots are the cells retrieved from the center (red dot) with a radius of 1.


GetRingPositions

public List<Vector3Int> GetRingPositions(Vector3Int centerPosition, int radius)

Returns a list of grid positions on a ring around a given center with a given radius. This method is useful if you want to do operations at all neighbour positions and don't care if cells exists or not.

Parameters

  • centerPosition: The center position of the ring.
  • radius: The radius of the ring from the center position.

SnapToCenter

public Vector3 SnapToCenter(Vector3 objectPosition)

Takes a transform position and returns a new world position at the center of the nearest grid position.

note

This approach assumes a center pivot and the size to be the size of a tile. Objects larger than this need to be adjusted manually

Parameters

  • objectPosition: The position of the transform to be snapped.

Example

public GameObject player;
//Snaps a player object to the center of its nearest grid position
player.transform.position = tilemapExtended.GridExtended.SnapToCenter(player.transform.position);

SnapToEdge

public Vector3 SnapToEdge(Vector3 objectPosition)

Takes a transform position and returns a new world position at the bottom left edge of the nearest grid position.

note

This approach assumes a bottom left pivot and the size to be the size of a tile. Objects larger than this need to be adjusted manually

Parameters

  • objectPosition: The position of the transform to be snapped.

Example

public GameObject player;
//Snaps a player object to the bottom left corner of its nearest grid position
player.transform.position = tilemapExtended.GridExtended.SnapToEdge(player.transform.position);

GetTileHeight

public float GetTileHeight()

Returns the height of the tiles / grid cells.


GetTileWidth

public float GetTileWidth()

Returns the width of the tiles / grid cells.