Instantly share code, notes, and snippets.

# onewinter/Hex.cs

Forked from inca/Hex.cs
Last active May 31, 2023 09:17
Show Gist options
• Save onewinter/023c78c2c3d3d98c92a49725e53ef2a8 to your computer and use it in GitHub Desktop.
[Unity] Simple Hex Grid System

# Simple Hex Grid System (forked)

The Hex.cs above has been filled in with more of the example code from https://www.redblobgames.com/grids/hexagons/; in addition, the code now works better with floating point hexagons. Much thanks to @inca whose original gist helped me finally understand & use hex grids!

Unity doesn't include C#'s PriorityQueue class yet; you can use this direct port from the official C# lib as a drop-in replacement: https://github.com/FyiurAmron/PriorityQueue/blob/main/PriorityQueue.cs

These two classes are the essential building blocks for building all sorts of hex-based grids, including but not limited to puzzles, tower defense, strategy and tactics games.

Important! Even though just copy/pasting the code will give you what you want, you won't be able to go much further unless you understand the bare essentials. I strongly recommend taking a cup of tea/coffee and reading the first half of https://www.redblobgames.com/grids/hexagons/ which explains pretty much everything you need to know about hex grids.

## Usage & Beyond

Copy and paste into your project. `Hex` is "model", i.e. it is a plain `struct` which doesn't derive from `MonoBehaviour`. Its purpose is to encapsulate a pair of integer coordinates in hex grid space (in skewed coordinate system) and to perform conversions to/from world space, alongside some basic traversal methods.

The `Node` class inherits from `MonoBehaviour` and therefore can be attached to any game object. When you do so, said object automagically begins to snap to a logical hexagonal floor, when you move it in your editor. Please note that by "floor" we mean world XZ plane by default; changing that is trivial by tweaking `WorldToPlanar` and `PlanarToWorld` static methods.

The dimensions of grid cell are chosen in a way that Unity's unit sphere (with radius `0.5f`) in inscibed into a unit hexagon of grid. Which means, the inradius of hexagonal grid is `0.5f`. It is relatively easy to modify this: you only need to modify 4 basis vectors (Q_BASIS, R_BASIS, Q_INV, R_INV) which are responsible for converting the coordinates.

`Node` also exposes a way to snap rotations to hexagonal dimensions, by using integer multiples of 60 degrees. By convention adopted from RedBlobGames the `dir` integer is constrained to `[0, 5]` range and is a universal way to address neighbours and all direction-related problems such as line of sight, traversal, pathfinding, etc. Our Hex Laser Puzzle game makes extensive use of that :)

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters