Finding distances between hexagonal tiles

I need to find the distance of hexagonal grids in numbers mathematically, something like this:

Example

I’ve actually created a script for this job, but it’s very slow, long and tends to break (Literally breaking the whole system, I’m rewriting the entire thing currently)

I did do research, but didn’t really find anything helpful to me
LMK if I missed anything!

Hi! May I ask you, how do you represent these tiles? Are they actual parts in the game? Can you please just give a bit more detail in that regard and I may be able to help you.

Yes, they’re actual parts (MeshPart)

The most annoying feature ever to exist

Check this resource out, btw is the distance horizontal or in any direction?

If its in ant direction try checking out the line drawing segment:

1 Like

Okay thanks. May I what you need to accomplish in this scenario so that I can more easily form a solution?

I tried looking into that page already, but I don’t really understand that much

And yes, all directions

Following the example, I will try to find the distance between one of the red colored tiles and the white tile (The one with the number 0) and it should return the number 1

Hi again!
If all you need find the distance from the white tile to any tile I would put an IntValue “Distance” and manually enter the value for every tile and then you can read it.
If you need different functionality let me know, and about my last response I should have been more specific, I was looking to understand what getting the distance would help you with.

No, that’ll take time

And it should work the exact same for every tile

local hex1, hex2 = script.Parent["1"], script.Parent["2"];


local dist = ((hex2.CFrame:ToObjectSpace(hex1.CFrame)).Position / hex2.Size);
print(math.round(math.abs(dist.Magnitude))) -- prints distance from hex2 to hex1
print(dist);

EDIT: It might add 1 to longer distances due to 1/2 of each start and end tile being omitted so just -1 if it’s greater than like 3 or something

1 Like

Thank you! I fixed it to function properly, thanks!

local dist = math.abs((hex2.CFrame:ToObjectSpace(hex1.CFrame). Position / hex2.Size).Magnitude)
dist = math.ceil(dist >= 3 and dist + 1 or dist)

print(dist)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.