HTML-page body

Document body

Do you have a question before buying? Check here to see if someone had the same question before you. If you don't find what you were looking for just ask, the more people ask the same thing, the more likely I am to add your question to the list as well.


Can I do...

Most questions asked can be answered by understanding what a grid in Grid Framework is: it is a grid in a mathematical sense, which means it is defined by an origin and some basic properties (like spacing for rectangular grids) and some visual properties. It is not a finite set of points in space. Here is a very rough sketch of what a grid class could look like; of course there is more to it in practice, but this gives you a good idea.

// Origin and rotation are inherited from the GameObject
class RectGrid : Grid {
    public Vector3 spacing;
    public Vector6 shearing;
};

This is what allows grids to be infinitely large in the game and very small in memory at the same time. All calculations are performed in constant time and cost the same regardless of where in the grid you are. The portion of the grid you see is just a finite-sized slice of what is infinitely large.

Does Grid Framework work with UnityScript?

Yes, UnityScript and C# get compiled to the same bytecode anyway. Grids are subclasses of Unity's Component class and can be manipulated like any other component.

var grid : RectGrid = gameObject.GetComponent.<RectGrid>();
grid.spacing = Vector3.one;

Can I store data in the grid?

A grid does not store any data in its vertices, cells or anywhere because it does not store any vertices to begin with. With that said, we can replicate this very easily. This code example creates a three-dimenstional array of world-positions of grid vertices. We will use a rectangular grid in this example.

int width, height, depth;
RectGrid grid;

// Change this to whatever type you want
Vector3[width, height, depth] vertices = new Vector3[width, height, depth];

// Loop over the grid
for (var i = 0; i < width; ++i) {
    for (var j = 0; j < height, ++j) {
        for (var k = 0; k < depth, ++k) {
            // store the world coordinates of the grid points
            vertices[i, j, k] = grid.GridToWorld(new Vector3(i, j, k));
        }
    }
}

You can then index the array using the grid coordinates of the vertex you want. We used a simple Vector3 for our array elements, but we could have used any custom data type as well.

What grids are supported?

At the moment rectangular, spherical, hexagonal and polar (cylindric) grids are supported. Rectangular grids can also have a shearig to slant them for an isometric look. Hexagonal and polar grids are two-dimensional and can be stacked on top of each other for the third dimenstion.

Which renderers are supported?

The renderers depend on the type of grid, with hexagonal grids providing multiple renderers.

  • Rectangular use the Parallelepiped renderer which renders the grid as a parallelepiped of adjacent parallelepipeds.
  • Spherical grids use the Sphere renderer to render a potentially partial spherical grid, such as the latitude and longitude grid on a globe. It can render spheres within spheres.
  • Polar grids use the Cylinder renderer to display a cylindric shape where the circular portion can have a starting and ending angle.
  • Hexagonal grids can rendered as a Cone, a possibly partial large hexagon made of many smaller hexagons. The name derives from the fact that this shape is often used in hex-based games to characterize a cone-shaped area of effect.
  • Hexagonal grids can also be rendered as a Rectangle where every odd column is shifted up- or downwards or clipped.
  • Hexagonal grids can be rendered as a Rhombus which can be slated up- or downwards.
  • Hexagonal grids can be rendered as a Herringbone pattern of edges connecting the central points of the hexes rather than drawing the hexes themselves.

Can I make irregular grid shapes?

Yes, there are multiple ways. The easiest way is to have multiple renderers per grid. Every renderer can then render a different finite part of the infinitely large grid.

If you wish for more control than that you will have to write your own renderer class. Grid Framework provides you with the same protected API as was used in writing the official renderers, you have the full power at your command.

Finally, you can do the rendering yourself or using a rendering plugin of your choice for Unity. That way you have full control over the shape. You can get the points of the lines to render use the grid's GridToWorld method:

Vector3 from, to;
RectGrid grid;

Vector3[2] line = new Vector3[] {grid.GridToWorld(from), grid.GridToWorld(to)};

This last approach should not be necessary in most cases, the renderer API is powerful enough.

What about irregurar grids like Voronoi grids?

No, a grid (also called a lattice) has to be regular by definition. Voronoi "grids" are not actually grids, they are graphs and finite in size and therefore not suitable for Grid Framework.

Is pathfinding included, or can I use a pathfinding plugin?

At the moment of this writing there is no built-in pathfinding, but using Grid Framework with a pathfinding plugin is easy. The plugin will need a list of vertices to contrustruct the nav-mesh from, and we can get these vertices from the grid by looping over the range we want to use:

int width, height, depth;
RectGrid grid;

Vector3[width, height, depth] vertices = new Vector3[width, height, depth];

// Loop over the grid
for (var i = 0; i < width; ++i) {
    for (var j = 0; j < height, ++j) {
        for (var k = 0; k < depth, ++k) {
            // store the world coordinates of the grid points
            vertices[i, j, k] = grid.GridToWorld(new Vector3(i, j, k));
        }
    }
}

These vertices are in world coordinates and can be then passed on to your pathfinding code.

Can I restrict movement or position to the grid?

Yes, the easiest way is to align the position to grid coordinates. Let's assume your object should fit into the centre of the nearest face. Here is the code using a rectangular grid:

Transform myObject; // Object to align
RectGrid  myGrid;   // Grid to align to

// Use common defaults
myGrid.AlignTransform(myObject);
// More control
myObject.position = myGrid.AlignVector3(myObject.position);
// Use grid coordinates
Vector3 gridPosition;
myObject.position = myGrid.GridToWorld(gridPosition);

As you can see there are multiple possibilities, depending on what suits you best. To restrict continuous movement apply the snapping of your choice on every frame in you Update() method.