I had a few technical questions regarding the tiles used by this approach, so I figured I would at least share my approach. It’s probably not optimal, but it works so far.
Q: So how do we create a Hexagonal Tile?
I use Blender to do all of my 3D modelling, so naturally I used Blender to generate a simple hexagon. So here are some quick steps to create a unit hexagon like the one seen here using Blender.
- Create a new empty scene in Blender.
- Set your camera to look down the z Axis (from the top) using NumPad+7.
- Add a Cylinder mesh with 6 vertices, with a depth of 0.2 (or however “thick” you want your tiles to be)
- By default, Blender generates it 15 degrees off center, so rotate the hexagon by -15 degrees around the z Axis to align the edges.
- Using this approach, Blender generates the hexagon with a Circumradius of 1.0, which is perfect. If you want a different value here, scale the entire model by whatever modifier you want, and that value becomes your new HexSideLength in the previous code sample.
- Now, to get this hexagon into unity, select only the hexagon object and choose: File -> Export -> Autodesk FBX (.fbx) and use the following settings:
- Save the fbx file somewhere in your Assets folder in your Unity project.
- Now, when you go back to Unity, you should see your model in the Project window. Simply drag and drop it into your scene to see it rendered in Unity! Or, you can set this object as your HexTilePrefab on the HexGrid script from my Hexagon of Hexagons post, and you can tile them in a rectangular grid.
Q: Is each Tile a prefab?
In my case, yes. Once I have successfully imported the model to unity, I create 1 prefab for each unique tile type that I want to use.
- In Unity, create a blank game object (Ctrl + Shift+ N) in the Hierarchy window.
- Drag the Hexagon FBX object onto the empty GameObject.
- In the Project view, create a new Prefab and call it whatever you want. (Tile_Blank for example)
- Drag the blank GameObject you created in step 1 with the child hexagon onto the Prefab in the Project window.
- Now you can drag that prefab into your scene, or set it as another script’s GameObject for tiling.
The other major benefit to doing this, is that you can add unique behavior to each prefab. If every forest tile acts the same, create a ForestTileBehaviour and attach it to your prefab. In this way, you can create a whole collection of unique tile prefabs, each with their own unique behavior.
Q: How can I select a hexagonal tile using this approach?
Generally speaking, when selecting something, I use a Raycast from the current camera to see if the ray has collided with any Colliders. So I simply attach a Mesh Collider to the Cylinder object, and now the Cylinder GameObject will register a raycast collision when the user clicks on this object using the mentioned script. What you do with it when you click on it is up to you!
Note: Be sure you attach the Mesh Collider to the object that has the Mesh Filter component, or you’ll have to manually assign the Mesh in the Mesh Collider, which causes other problems with rotations.
That’s all for this post. If there are any more specific questions, I can try to answer them with another post. Maybe I’ll actually work on the project again soon too.