# Generate procedural hexagonal tiles around a player at runtime

While I was looking into making a grid around the player, I wanted to have the grid generate around the player. However, I can’t think about the right approach to achieve this effect. With squares it is rather simple. but hex shapes are giving me some trouble to generate correctly. I have written my code to draw a grid for now, since drawing it around the player is the same thing but with a constraint.

The question is, what would be the best approach to generate such a grid. I am going to generate a 3-wide area around the player and hide what is not in range (I will make this a variable and upgradeable because I am making a rogue lite).

The reason I am using hexes, is because that makes the distance to the edges equal, making it easier to calculate, but it is causing me different issues.

Question 1: Do I use an overlay collider to hide everything (clip) everything outside the area or do I calculate the distance to the pieces and decide based on that what should be generated/visible/hidden?

Question 2: How do I efficiently generate a tile pattern like this (I laid them out to show what I am trying to do). And possibly recursively generate more when the players render distance changes.

I am using procedural generation in my 2.5d shooter for background structures, bases and asteroids. The best resource I found for learning about hex grids can be found here: Hex Map 1

1 Like

I’ll try getting that up and running. using prefabs instead though since not all tiles are fully procedural. Should still be a relevant topic. thx!

Hmm this approach works for an entire grid, but I am trying to draw it circular around the player… I cant seem to apply the same generative logic as he is doing. I am going to try it using trigonometry and calculating the new positions based on the player as the center point. I think for my case this should work. It did point me in the right direction.

Edit: My generation pattern is circular, and thats where I am stuck right now… If anyone knows an algorithm for that. that’d be very helpful since I am currently trying to deduce it myself.

I just found this How to generate list of hexagonal coordinates? - Questions & Answers - Unity Discussions. Might be useful for reference, if anyone comes accross this post wondering how to do it. This is more in-line with what I am doing.

2 Likes

Does this grid follow the player?

I have made a grid of my own, but its a bit different, it generates it for the whole map, with a slight tweak, you can make it so that it only grows when your character collides/activates the hex nearby.

Mute the video, I made it coz I needed help with something a while back, just wanted to show u the grid

https://www.redblobgames.com/grids/hexagons/
May be of interest to you as well.

@Mortalanimal this grid will follow the player in the sense that it will be generated according to the player position.

So I managed to pull it of with some rotational magic
Essentially what I am doing is generate an object next to the original and rotate the object generated around the parent. I do that 6 times for the first layer (60 degree rotation because hexagons). After that I do the same thing but 12 times with a 30 degree angle. And set the position of j % 2 == 1 to a corrected value using the innerRadius / 3. This is working rather well. I do not like the way this is essentially two times the same code… But I dont know how I can make it a little more compact. Any ideas on that?

``````public class MapManager : MonoBehaviour
{
[AssetsOnly] public GameObject _prefab;

[SerializeField] private float gap = 0.1f;

private GameObject startPos;

void Start()
{

startPos = Instantiate(_prefab, Vector3.zero, _prefab.transform.rotation);

for (int i = 0; i < 6; i++)
{
var temp = Instantiate(_prefab, new Vector3(0, 0, innerRadius + gap), _prefab.transform.rotation);
temp.transform.RotateAround(startPos.transform.position, Vector3.up, 60 * i);
temp.transform.rotation = _prefab.transform.rotation;
}

for (int j = 0; j < 12; j++)
{
var temp = Instantiate(_prefab, new Vector3(0, 0, (innerRadius + gap) * 2), _prefab.transform.rotation);
if (j % 2 == 1)
{
var pos = temp.transform.position;
temp.transform.position = pos;
}

temp.transform.RotateAround(startPos.transform.position, Vector3.up, 30 * j);
temp.transform.rotation = _prefab.transform.rotation;
}
}
}
``````

Edit: It does however not work properly on the second circle with the offset. its slightly wong.

I cant seem to find why these gaps are being placed wrong. Seems kind of odd to me.
I just double checked the maths and it seems to line up for me… Any input on that aspect?

Even without the gaps the distances are slightly wrong…