Learning Need Help With Hexagonal Tile Maps

Hello I’m new to the unity platform and require advice or a place to learn what I need to do.

To explain I need to create a hexagonal grid for my game where I can drag and drop modules into the the grid and they connect and talk with one another exchanging variables with each other. also there needs to be a creation method for the modules to drag and drop multiple into place. my skills with C# are limited but I am very willing to learn what I need to make my dream come to life.

Thank you for your time.
BJW

First off, the kind of thing you want to do is probably more difficult than you expect especially for a beginner to C# and Unity. (Your post could be more specific, but I’m imagining something in management or building genre?) This is because a lot of the beginner Unity tutorials you may find online won’t cover the C# stuff so well. If anything, you should invest time to studying C# for a while and how to organize a small project. That said, I’m going to type out my advice, which you may need to come back to once you’ve have more practice.


In each cell of the tilemap, there’s not much information that can be placed. To have something like modules that exchange information with one another in adjacent cells, you will need an independent data structure to store that kind of information.

For example:

class Map
{
   Dictionary<Vector3Int, Module> extraData = new blahblah;
   Tilemap tilemap;

   void SetCell(Module m, Vector3Int cell)
   {
       tilemap.SetTile(cell, m.GetTile());
       extraData.Add(cell, m);
   }
  // when destroying a module, clear it from both locations too
}

This isn’t complete code but aims to illustrate an important point: you have to keep parallel stores of data. The tilemap can represent the visual aspect of a module, whereas the extraData is where you keep a record of what Module is in each cell. When setting a tile on the tilemap, you need the parallel extraData. Upon hitting Play within the Editor, you’d create the map and populate it with data.

How could Modules exchange information with one another?

class Module
{
   Vector3Int myCell;
   Map map;

   public void CheckNeighbors()
   {
       List<Vector3Int> neighbors = GetNeighboringCells(); // get hex positions
       foreach(var cell in neighbors)
       {
           Module neighbor = map[cell];
           var info = neighbor.GetInfo();
           var change = EvaluateNeighborInfo(info);
           this.ModifySelf(change); // based on neighbors, change this module in some way
           neighbor.ReceiveChange(change); // based on neighbors, change neighbors in some way
       }
   }
}

Again, not complete code, but shows how you could plausibly get and receive information from neighboring modules: by asking the Map for what’s in the cells of the neighbors.


Then there’s the question of how to “drag and drop” presumably from UI then onto the tilemap.

In particular, there’s WorldToCell and CellToWorld methods. These allow you convert from the world position to the cell position and vice versa. You can add a module to the map if you can get the position of the mouse. That’s Input.mousePosition. That, however, is in screenspace. So, we have 3 coordinate spaces we have to translate between: screenspace → worldspace → cellspace. You can obtain the worldspace coordinates of the mouse cell by using: Camera.main.ScreenToWorldPoint(Input.mousePosition).

Vector3Int GetCellUnderneathMouse()
{
    Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    worldPos.z = 0f; // clear z b/c otherwise has camera's
    Vector3Int cell = tilemap.WorldToCell(worldPos);
    return cell;
}

Anyway you asked a very large question and I didn’t even get to all the bits of it. So what you should do is to try to break off one of these things. I’d recommend starting by building a little in-game system to drag-and-drop tiles onto the tilemap. Later you could add the parallel data structure to store information about the modules.

Thank You Lo-renzo I will take your advice on board and do more learning on C# before going out and trying to make the final idea happen. As you said break it down into chunks and slowly add and refactor the code to get it where I need it to be for my idea. Thanks again Lo-renzo.