devlog #3 // hex tilemaps and pathfinding, part 2 (setting up a procedural map)
okay. procedural map generation, here we go!
last time, we talked through some of the options available for making tilemaps, namely hand-crafting maps or generating them more procedurally. Â after discussing a few benefits and challenges for both, i decided that it might make the most sense to start with the procedural side of things, so thatâs what weâre doing today.
it all begins with one hex -- and a lovely tutorial.
i crafted this cutie in blender (a free modeling and animation tool). first, we make a cylinder and reduce its vertices to just 6, defining sharp edges. after that, we make a loop cut near the top, then scale down those vertices to add a bevel. just something simple to start with. export!
when bringing the model into unity, blender models seem to do lots of wild things, namely around rotations and scaling (for example, showing the hex as rotated 90 degrees on its side). Â
the model iâm using changed its orientation, position, and scale multiple times as i set it up, so rather than continue to mess with these discrepancies (namely differences in how blender and unity handle coordinate data), i decided just to orient and scale the model exactly as i desired, then child it to an empty gameobject. making this nested (an adjusted child object inside a neutral parent object) and a prefab (a template i could make endless copies from) assured that the tilemapsâ setup and calculations would be much simpler.
this image shows a few of the nested prefabs together, just to get an idea <3
here comes the scary part:Â
t h e C 0 D 3
i included (helpful?) comments in most of my coding today, so you can look more closely at those, but the basic procedure for setting up the initial map was as follows: (1) define that there will be a gameobject weâll need a reference to - our hex prefab!
(2) define a map size as a vector2; in the tutorial linked above, quill does this in two different variables, but i donât see why using a vector2 would cause any problems and itâs cleaner, so weâll see
(3) in the start function (aka when i press play on the game), execute a for loop that goes through each of the x-y coordinate positions and instantiates (creates) a hex there note: our y coordinate is placed in the 3rd position of the Vector3, which actually represents the z-axis. i set up the coordinates this way in order to preserve our y-axis as vertical. things can certainly be set up differently, and the first few times i programmed the coordinates this way, it used to confuse me quite a lot :o
from there, we get something that looks like this:
so, not exactly ideal-appearing yet, but really quite close to what we need. it may be difficult to identify just by looking, but whatâs happening is that the tiles are overlapping because our code is not adequately describing how much space to leave between them. for this, we set up offsets on each axis!
at first, i added multipliers in around the instantiate call so that the tiles would be spaced further apart when created, but i quickly realised this was not a very clean solution. instead of trying to figure out âokay how much do i need to modify the x position, due to the size of the tile?â and hard-coding in those values, i decided to adjust the hex model (inside the prefab) to something that would allow the tiles to be centered at whole numbers (1, 2, 3..) along the x-axis. although there were still hard-coded values for the offsets, these should ideally never change and the rest of our code remains clear, without any surprise, unexplained numbers.
once adjusted, this code results in quite a pleasing set of tiles, or as pleasing as a plain white geometry can be:
and as mentioned before, this gave us cleaner positions along the x-axis, something i hope will be helpful later:
here, i decided to implement a few mostly cosmetic changes, to help things be more organised in the hierarchy - childing every created tile under the map object (which is where the map-generating script is placed) and giving each one a name based on its tilemap coordinates:
so, now that we have these tiles set up, how do we actually .. get them to .. do things? how do we interact with them?
well, following quillâs suggestion in the tutorial series, i create a mouse manager (a script i attach to an object in our scene) which will help us interact with the tiles.
ignore the "Color lightPurpleâ line for a second; really, the magick starts with our ray. this is an invisible line which begins at one point and is cast out in a direction, for a certain distance (or eternity!). fortunately, unity offers us the function of âCamera.main.ScreenPointToRayâ which can take our âInput.mousePositionâ and draw a ray between the main cameraâs location and the screen coordinates where our mouse is placed. in other words, we are a little closer to being able to click on things! once we send out the ray, it says âi hit somethingâ or âi didnât hit anythingâ - if it does hit something, we give it the data structure of âhitInfoâ and say âhey, fill this out so that we can know what the ray hit.â since this is currently in the update function, we will get this information back every single frame. every frame that our mouse is hovering over/touching something with a collider, our code will tell us the name of that object.
then, lastly, we set up to receive mouse input - aka clicks. this code says âwhen i let up from a left mouse click, grab the mesh renderer component on the ray-touched object, then change its color! thatâs why we defined a color up top, in preparation of having many custom colors we might specify in the future.
and this is what that code looks like in action, as far as the clicking:
and lastly, a pretty view of our geometry <3
so, in summary, today we:
- created a hex prefab with a non-absurd rotation and scale
- set up a grid of those hexes which will automatically be spaced out appropriately
- made it so that we can hover or click on a tile and not only get back information on which tile it was, but also effect changes to that tile
whew.
weâre well on our way.
in the next devlog, letâs aim to set up more complex click interactions (like selecting tiles for movement, toggling colors, hovering one color but clicking to change) and some basic pathfinding to be able to move our player from one space to another. maybe weâll even explore a bit of customising tile colors, heights, and textures to create biomes .. or perhaps save that for another time. with love and a hex-based personality,
ahn












