Wednesday, 14 January 2015

First Steps with Unity - Hexagons

This is the first in a series of posts about the development of the electronic incarnation of Essence of Glory.

We are just beginning work on this game, and we're using the Unity engine. We chose Unity for its availability, ease-of-use, and wide variety of deployment platforms.

This week's post will mostly be about hexagons.

What's so great about hexagons? Well, as the logo at the top of the page may suggest they are a pretty significant part of a lot of tabletop and wargames. They provide a more accurate and natural way of moving game pieces around than a square grid. However, they are a little trickier to draw than a grid of squares.

Unity measures the position of game entities with a three-dimensional vector. Our game board will mostly be flat, so we can ignore the height dimension for now. That leaves us to consider position on a two-dimensional plane, like a cartesian co-ordinate plane.

Remember math class?
As you might imagine, it's rather easy to draw squares on such a grid. The grid is already based on squares! Just draw a square at every position (x,y) and there you have it.

But how do you draw a grid of hexagons?






Well, it might help to imagine them as a grid of squares where every second column (or row, depending on your perspective) is offset a little.

Notice how each tile has 6 neighbours touching it.
What we want is a way of numbering hexagon tiles so that we can easily determine where to draw them.

Seeing as a hexagon grid is analogous to a square grid with offset rows, it may be tempting to try to number it like a square grid. Depending on how this is done, the results may not be helpful.

If you try to keep the axes at 90 degrees to each other, you may end up with something like this.

x-axis in red, y-axis in blue
As you can see, the y-axis is really ugly. Converting from these co-ordinates to Unity's world co-ordinates is not too difficult, but the reverse is a bit of a pain. What we want is axes like this:

axes as above
Much better. Converting from these hex co-ordinates to world co-ordinates and back will use the same formula at every position.

Now, to implement this in Unity, I followed the approach described here. As a programmer, I am much more comfortable with creating and modifying game objects from scripts rather than using Unity's drag and drop features. I won't go into detail here, but I first created a HexManager script, which spawns new game objects and attaches the HexModel script to them. Ideally, I would like to create a prefab out of the hexagon objects, as the game board will likely be the same in every level, but this is a useful construct and will do for now.

So, we have a way of drawing hexagons where we want them, and a logical way of describing where that is. Next week, we will be working on adding some interactivity to this video game, getting something to happen when you click on these hexagons.

In addition the article linked above, I also found 'Hexagon grid: Generating the Grid' and 'Hexagon grids: coordinate systems and distance calculations' to be of the greatest help.

No comments:

Post a Comment