So I’ve been investigating and poking the code around in the tutorial I found here.
I’m actually beginning to understand how it works and it’s actually a great piece of code for making grids. However I suspect I’m reading it wrong because I’m poking at it and trying to change the position itself it instantiates to but I’m having a hard time doing so. There is clearly some form of code that’s registering the position since it always goes to the exact same space.
What would make this code perfect for me however if there was some way I could actually parent the entire grid to an empty object and then move it around at will without having to screw around with custom coordinates constantly.
If somebody could explain the positioning to me I’d be grateful, so far I understand how the offsets are to do with spacing the gaps between the hexagonals and naturally the height and width of the first integers are for the height and width of the actual grid itself I just need to find out how to tweak the position of the entire grid itself.
Ohhh!!! So if I change the empty to “HexGrid” then that should make everything work right? Christ, there’s so much code there I totally missed it, thought it would be much more complicated.
Nope, jumped ahead of myself, not as simple as that.
void createGrid()
{
//Game object which is the parent of all the hex tiles
GameObject hexGridGO = new GameObject("HexGrid");
for (float y = 0; y < gridHeightInHexes; y++)
{
for (float x = 0; x < gridWidthInHexes; x++)
{
//GameObject assigned to Hex public variable is cloned
GameObject hex = (GameObject)Instantiate(Hex);
//Current position in grid
Vector2 gridPos = new Vector2(x, y);
hex.transform.position = calcWorldCoord(gridPos);
hex.transform.parent = hexGridGO.transform; // <<< Found the exact source of the problem
}
}
}
It’s clearly this little annoying piece of code that’s causing it, what happens is, it creates an entirely new object so even if I attach the script to my empty object It still goes ahead and creates an entirely seperate object where all the hexagonals attach to.
So as you say I think what I need to do is write a seperate transform.parent piece of code to attach the entire grid to the empty so I can move it around easily. Or? Re-purposing the code to parent to a public game object might actually work better for my needs.
I think I know what to do now, will try it and see if it works.
Hmmm >_< I’m trying to get a public or private GameObject set up at HexGridGO because that seems the most logical place to put it but no matter what I do the compiler keeps on complaining about me using public/private GameObject to try and get this particular piece of code to look for “HexGrid” in the inspector.
Even removing the tag altogether doesn’t seem to really do anything, surely I should just be able to make a public game object at the top and link it to hexGridGO right? But obviously that isn’t working I wonder if it’s because of the void CreateGrid function cutting things off perhaps.
What I think you are doing your just adding “public” to line 4 in your code?
If you are doing that remove line 4 completely and make a public variable at the top of the class (outside of methods) with the same name. Then assign the empty object in the inspector.
I gave it a try and it seems to be detecting it fine, but even though the hexagonal grids are being parented now the position itself stays exactly the same.
Edit: Oh no! I spoke too soon! still doesn’t seem to be quite working, still seems to be stubbornly staying in one spot regardless of it being parented.
Bah, frustrating -_- it’s definitely this piece of code that’s messing with the positioning though.