Hex Grid Movement in Unity 2021 P1 - Creating a hex grid

  Рет қаралды 38,159

Sunny Valley Studio

Sunny Valley Studio

Күн бұрын

Пікірлер: 42
@SunnyValleyStudio
@SunnyValleyStudio 3 жыл бұрын
Part 2 - > kzbin.info/www/bejne/jXjSYWqagq-Dqs0
@abhinavangirekula3339
@abhinavangirekula3339 4 ай бұрын
Such a well-made and comprehensive video. The way you have these slides to explain how hexagons change the fundamental logic of tile coordinates math, and how you have these well made diagrams to illustrate what you're talking about: incredible! I'm very new to Unity, and I've grown so much as a developer literally from just watching this video, making my own optimizations, and debugging the errors that pop up from my mistakes. One 20 minute video opened the door to so much more. Thank you!
@SunnyValleyStudio
@SunnyValleyStudio 4 ай бұрын
Glad it was helpful! 🙂
@개복치-s2w
@개복치-s2w 3 жыл бұрын
Wow! I desperately need this. Thank you!
@개복치-s2w
@개복치-s2w 3 жыл бұрын
I enrolled your 2d platformer and juicy course!
@Themok-Gamma-6
@Themok-Gamma-6 3 жыл бұрын
Looking forward for next video with movement.
@SunnyValleyStudio
@SunnyValleyStudio 3 жыл бұрын
Thanks! Just uploaded the next part kzbin.info/www/bejne/jXjSYWqagq-Dqs0
@bamboy420
@bamboy420 2 жыл бұрын
This is a good introduction to creating hex-based systems, but using a separate class instance for each hex absolutely cause your game to chug if you use larger maps. Would be more performant and scalable to create and manage your hex data using structs.
@SunnyValleyStudio
@SunnyValleyStudio 2 жыл бұрын
Thanks for the feedback!
@KitchenTableMaths
@KitchenTableMaths 2 жыл бұрын
I've seen people mention this on a few hex tutorials. I'm pretty new to developing so I'm unclear on how this is done? Do you know of any good instructional videos/articles on how it works?
@bamboy420
@bamboy420 2 жыл бұрын
@@KitchenTableMaths I would suggest starting this with a square grid before attempting a hex grid because of the added complexity, but basically you will want a central "map" class that can convert a position to and from world position and cell position, as well as holding cell positions of the objects that are located somewhere on your map. In my recent project I used a Dictionary for this, where MyMapObject is the (abstract) base class of any of your game pieces. Basically the idea is you want to track where the important game related objects are at any given time. Obviously you will want to make sure that MyMapObjects manage their own dictionary entries carefully as they change cell positions or are created and destroyed. This approach isn't really specific to Unity but to be honest Unity doesn't have a good built-in approach for these sorts of things anyway.
@dejo095
@dejo095 3 жыл бұрын
Cool! Ill check this later, thanks for upload
@SunnyValleyStudio
@SunnyValleyStudio 3 жыл бұрын
Thanks!
@xerpenta
@xerpenta 9 ай бұрын
The vertical offset depends on how many rows you need, since the 1,73 is a rounded square root of 3. If you are building a strategy game that can have 200 or 300 rows, missing the next digit in sq.rt. of 3 which is 2, will cause you to lose 0,1 every fifty rows and so the last row will be misaligned by 0,4 or 0,6 respectively.
@SunnyValleyStudio
@SunnyValleyStudio 9 ай бұрын
I didn't think about that. Thanks a lot for pointing that out! Right now I would use docs.unity3d.com/Manual/class-Grid.html component and use that to calculate the positions to create the map. Probably it would also be better to have a single big mesh and just create highlighted hexes as additional mesh where I want to highlight the path. I might need to update this tutorial 🙂
@abhinavangirekula3339
@abhinavangirekula3339 4 ай бұрын
I have a really weird issue with the z-coordinates where no matter which tile I select, it will output 0 as the offset z value. I've walked through my code and troubleshooted for hours, but I still can't figure out why it does this. X-coord works perfectly(the ceil thing since hex rows are weird is a very smart catch). When I use Debug.Log, it shows that the value of z being returned by ConvertPositionToOffset and in the offsetCoordinates vector is the expected value per tile, but when I actually run the game, every tile ends up with z = 0. EDIT: Y'all aren't going to believe how stupid I was. I was looking at the Y-Coordinate and mistaking it for the Z-Coordinate. You know, the y-coord which IS MEANT TO ALWAYS BE ZERO. My bad. Thanks for the video!
@SunnyValleyStudio
@SunnyValleyStudio 4 ай бұрын
I'm glad that you were able to fix it 👍
@scottpayne1091
@scottpayne1091 2 жыл бұрын
Great tutorial but I am failing to follow your steps, Visual Studio does not give me the same "Quick Actions and Refactoring" as shown in the video, any advice?
@SunnyValleyStudio
@SunnyValleyStudio 2 жыл бұрын
Hey! In microsoft visual studio 2019/2022 you should be able to right click and at the top of a new window be able to access quick help / refactoring options. Here is the documentation that describes this in more detail learn.microsoft.com/en-us/visualstudio/ide/reference/extract-method?view=vs-2022 You can do the same that we do by typing the code by hand. Still most IDE has some helpful macros that are worth leaning (can save you time when coding).
@javhm1611
@javhm1611 3 жыл бұрын
10:30 Why the child of Main dissapear? It works for me the part that the father is selected.
@SunnyValleyStudio
@SunnyValleyStudio 3 жыл бұрын
Hey! Hex is a prefab. In Main we put in the mesh representing the hexagonal model underneath. In the props we put the trees, buildings or whatever you need to have on top. This is just to make the hex prefab more reusable. Maybe my original prefab has no default mesh - maybe it should be the simple grass mesh. Sorry about not mentioning that!
@TimurKOI
@TimurKOI 2 жыл бұрын
Hmmm... I have a problem with snap settings: I created a model in Blender, placed it at the same position, as you. But when I typed the same options in snap settings as you did there is a huge gap between hexes. When I changed the number 1.73 in x axes it worked well in horizontal axes but it didn't work well for vertical axes. What've I done wrong?
@SunnyValleyStudio
@SunnyValleyStudio 2 жыл бұрын
Hey! It might be a problem where your pivot point for your hexes was set in blender in the origin point while you have created the model with an unintended offset? Anyhow you should be able to simply create a parent object in Unity and place your model in it so that it has the same pivot as the hexes that I am using. This should mitigate any unwanted offset (but you might have to modify some scripts to try accessing the child object instead of the parent without the hex mesh / material)
@TimurKOI
@TimurKOI 2 жыл бұрын
@@SunnyValleyStudio oh, thank you very much. I'll try to fix axes in blender. I"ve already tried to parent created hex to empty object in unit. Result was the same. I"ll let you know if I succeed. Anyway, thank you very much for your detailed answer. )
@stevenbitz4621
@stevenbitz4621 Жыл бұрын
I had the same issue. Had to modify the HexCoordinates script with the following: xOffset to 1.74f, and the zOffset to 1.52f. Everything works now, except the mouse click doesn’t register tiles in the -x or -z directions. Still trying to figure it out. Hope this helps.
@derkth3793
@derkth3793 4 ай бұрын
Hi, I know it's been a while but I have a question, why is the hexTileNeigboursDict a dict Dict < starting position , list of neighbours position > instead of a : Dict < starting position , list of hexesTiles > ? Is there a specific reason?
@SunnyValleyStudio
@SunnyValleyStudio 4 ай бұрын
Hey! I think its just for making it easier to run an A* algorithm but I don't remember exactly. Probably my thinking went "We have a grid. Grid cares only about positions. Tiles are a separate object so I will keep grid data in 1 data structure and Tiles data in a separate". Why? I guess it helps with keeping the code alighted to Single responsibility principle. Or at least that is how I would tackle this now :)
@HazebertM
@HazebertM 3 жыл бұрын
Does this work in 2d as well?
@someone.9290
@someone.9290 2 жыл бұрын
ConvertPositionToOffset() doesn't work for me, Ceil to int fails 30% of the times, and floor to int fails always, any other methods?
@SunnyValleyStudio
@SunnyValleyStudio 2 жыл бұрын
I'm not sure. It works for me fine but the calculation is dependent on the initial position of hexes and there size 9the static variables). You can implement your own calculation using the awesome guide by red blob games www.redblobgames.com/grids/hexagons/
@Jeppx7
@Jeppx7 2 жыл бұрын
I had some issues as well but used a hybrid solution that has been working so far. int x = Mathf.CeilToInt(position.x / xOffset); int y = Mathf.CeilToInt(position.y / yOffset); int z = Mathf.RoundToInt(position.z / zOffset); I use RoundToInt when calculating the 1.73-offset. Otherwise there can be a small error in the Ceil math that propagates if you have many rows or columns. Hope it helps!
@someone.9290
@someone.9290 2 жыл бұрын
@@Jeppx7 thanks man! although i don't need it now but maybe in future!
@xFafo92x
@xFafo92x 2 жыл бұрын
​@@Jeppx7 thank you!
@blainelight
@blainelight Жыл бұрын
@@Jeppx7 I think the exact math is sqrt(3) - which is 1.7320508076. not sure if that helps :)
@callmedeno
@callmedeno 2 жыл бұрын
Grid and snap settings seems to have disappeared in unity 2021?!
@SunnyValleyStudio
@SunnyValleyStudio 2 жыл бұрын
You can find how to set them in 2021 here docs.unity3d.com/2021.2/Documentation/Manual/GridSnapping.html
@HorizonSinistre
@HorizonSinistre 10 ай бұрын
BE VERY WARY: Selecting multiple tiles then placing them using the grid will not default to their center position relative to the grid, which means you might have to rearrange by hand all the tiles that were misplaced using group selection!
@SunnyValleyStudio
@SunnyValleyStudio 10 ай бұрын
Thanks for pointing this out! 👍
@oldshamen
@oldshamen 3 жыл бұрын
Tried to make a comment to direct people to some more information about Hexagonal Grids that would be interesting to follow after learning from this tutorial. Not sure if the comment was deleted because they contained a link.
@SunnyValleyStudio
@SunnyValleyStudio 3 жыл бұрын
Hey! I am not deleting anything so unless some YT settings does that automatically (no idea) feel free to post any links that you think can be useful to others :) I found redblob games article usefule (although a bit hard to follow because of the Y axis going down in their examples) www.redblobgames.com/grids/hexagons/
@Speeds_Gaming_Corner
@Speeds_Gaming_Corner 2 жыл бұрын
EDIT: Found the solution, I have missread the code and was using a hexTileNeighborDict where I needed a hexTileDict Encountering KeyNotFoundException errors when bringing in the HexGrid script. HexGrid.GetNeighborsFor (UnityEngine.Vector3Int hexCoordinates) (at Assets/Scripts/HexGrid.cs:40) HexGrid.Start () (at Assets/Scripts/HexGrid.cs:17) 10-23 is void Start() { foreach(Hex hex in FindObjectsOfType()) { hexTileDict[hex.HexCoords] = hex; } List neighbors = GetNeighborsFor(new Vector3Int(0, 0, 0)); //this is line 17 Debug.Log("Neighbors for (0,0,0) are: "); foreach(Vector3Int neighborPos in neighbors) { Debug.Log(neighborPos); } } and at 32-52 I have public List GetNeighborsFor(Vector3Int hexCoordinates) { if(hexTileDict.ContainsKey(hexCoordinates) == false) { return new List(); } if(hexTileDict.ContainsKey(hexCoordinates)) { return hexTileNeighborsDict[hexCoordinates]; //this is line 40 } hexTileNeighborsDict.Add(hexCoordinates, new List()); foreach(var direction in Direction.GetDirectionList(hexCoordinates.z)) { if(hexTileNeighborsDict.ContainsKey(hexCoordinates + direction)) { hexTileNeighborsDict[hexCoordinates].Add(hexCoordinates + direction); } } return hexTileNeighborsDict[hexCoordinates]; } Not sure how to go about trying to solve this error. Commenting out line 40 return is making it not run into errors, but the it just prints out the Debug.Log for "Neighrbors for (0,0,0) are: " one time and doesn't relay neighbors.
@NicholasBacon-oh9ju
@NicholasBacon-oh9ju Жыл бұрын
I am so glad you commented this and edited it to reflect what you found. Thank you!
Hex Grid Movement in Unity 2021 P2 - Highlighting hex tiles
24:44
Sunny Valley Studio
Рет қаралды 12 М.
Pathfinding - Understanding A* (A star)
12:52
Tarodev
Рет қаралды 141 М.
Гениальное изобретение из обычного стаканчика!
00:31
Лютая физика | Олимпиадная физика
Рет қаралды 4,8 МЛН
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 23 МЛН
Generating A Hex Map With Fog Of War in Unity
20:27
Game Dev Guide
Рет қаралды 55 М.
Procedurally Generating A Hexagon Grid in Unity
11:29
Game Dev Guide
Рет қаралды 124 М.
[Unity3D Hex Map Game Dev] 0: Introduction to Hex Maps
5:13
Re: cOg Mission
Рет қаралды 40 М.
Everything You Need To Start Making Games (As A Beginner)
16:11
Juniper Dev
Рет қаралды 131 М.
A new way to generate worlds (stitched WFC)
10:51
Watt
Рет қаралды 551 М.
Unity Tilemaps - Hex, rectangle, isometric + rule tiles
18:38
Гениальное изобретение из обычного стаканчика!
00:31
Лютая физика | Олимпиадная физика
Рет қаралды 4,8 МЛН