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

Поділитися
Вставка
  • Опубліковано 10 гру 2024

КОМЕНТАРІ • 42

  • @SunnyValleyStudio
    @SunnyValleyStudio  3 роки тому

    Part 2 - > ua-cam.com/video/WGo07dMJPtk/v-deo.html

  • @abhinavangirekula3339
    @abhinavangirekula3339 4 місяці тому +1

    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!

  • @개복치-s2w
    @개복치-s2w 3 роки тому +6

    Wow! I desperately need this. Thank you!

  • @xerpenta
    @xerpenta 8 місяців тому +1

    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  8 місяців тому

      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 🙂

  • @bamboy420
    @bamboy420 2 роки тому +6

    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 роки тому +3

      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 роки тому +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.

  • @개복치-s2w
    @개복치-s2w 3 роки тому +2

    I enrolled your 2d platformer and juicy course!

  • @Themok-Gamma-6
    @Themok-Gamma-6 3 роки тому +3

    Looking forward for next video with movement.

    • @SunnyValleyStudio
      @SunnyValleyStudio  3 роки тому

      Thanks! Just uploaded the next part ua-cam.com/video/WGo07dMJPtk/v-deo.html

  • @dejo095
    @dejo095 3 роки тому +1

    Cool! Ill check this later, thanks for upload

  • @abhinavangirekula3339
    @abhinavangirekula3339 4 місяці тому +2

    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!

  • @javhm1611
    @javhm1611 2 роки тому +1

    10:30 Why the child of Main dissapear? It works for me the part that the father is selected.

    • @SunnyValleyStudio
      @SunnyValleyStudio  2 роки тому +1

      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!

  • @scottpayne1091
    @scottpayne1091 2 роки тому +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).

  • @HorizonSinistre
    @HorizonSinistre 9 місяців тому +1

    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!

  • @derkth3793
    @derkth3793 3 місяці тому

    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  3 місяці тому

      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 :)

  • @TimurKOI
    @TimurKOI 2 роки тому +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 роки тому +1

      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.

  • @someone.9290
    @someone.9290 2 роки тому +1

    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 роки тому +1

      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 роки тому +1

      @@Jeppx7 thanks man! although i don't need it now but maybe in future!

    • @xFafo92x
      @xFafo92x Рік тому

      ​@@Jeppx7 thank you!

    • @blainelight
      @blainelight Рік тому

      @@Jeppx7 I think the exact math is sqrt(3) - which is 1.7320508076. not sure if that helps :)

  • @oldshamen
    @oldshamen 3 роки тому +2

    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 роки тому +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/

  • @callmedeno
    @callmedeno 2 роки тому +1

    Grid and snap settings seems to have disappeared in unity 2021?!

    • @SunnyValleyStudio
      @SunnyValleyStudio  2 роки тому +1

      You can find how to set them in 2021 here docs.unity3d.com/2021.2/Documentation/Manual/GridSnapping.html

  • @HazebertM
    @HazebertM 3 роки тому +1

    Does this work in 2d as well?

  • @Speeds_Gaming_Corner
    @Speeds_Gaming_Corner 2 роки тому +1

    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!