Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

FX Hex Grid (Hex Grid Generator)

Discussion in 'Assets and Asset Store' started by ForceX, Oct 10, 2011.

  1. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    I wrote a simple method to get the world space of a hexagon from their grid space.

    I'm not sure how they are named in the unmodified version, but I changed each hexagons' name to include their grid location. Then when I want to spawn a unit I call the method and it finds the hexagon with that grid location and gets that hexagons' world space. Then you spawn the unit at that location.

    I don't have any code to paste as I've heavily modified my version so it may vary.

    ForceX, sorry about the strange form of a thread hijack, I can stop if you would like. ;)
     
  2. kelbirk

    kelbirk

    Joined:
    Jun 27, 2013
    Posts:
    5
    So you attach the units as public variables to the main script, instead of inserting them into a unit script or the like?
     
  3. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Kinda I made a script just to spawn units into the map by using the above methods.
     
  4. kelbirk

    kelbirk

    Joined:
    Jun 27, 2013
    Posts:
    5
    My inexperience with Unity is showing, but how do you call units from a script to spawn in the game? Are the units already in the hierarchy and you just call them into the map at the appropriate time, or are you actually instantiating them wholesale from the script?
     
    Last edited: Dec 17, 2013
  5. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Lets break it down.

    I have a script "Spawner" and it has a public GameObject unit. When you want to spawn a unit you call a method that finds the hexagon(by searching for its name+grid position) and gets its "transform.position". Then you Instatiate the unit gameObject into that location from the hexagon's position.

    Edit: If your still confused you can add me on Skype and I'll try to help you tommorrow as it's sleeping time here. ;)
     
  6. kelbirk

    kelbirk

    Joined:
    Jun 27, 2013
    Posts:
    5
    Okay, I think I get what you mean. Thanks. I'm going to give it the ol' college try and we'll see how it works.
     
  7. thormodsen

    thormodsen

    Joined:
    Nov 21, 2012
    Posts:
    2
    Hi and thanks for sharing these scripts. One question though: why the second cam?
     
  8. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    It's to simulate edge wrapping.
     
  9. Sartoris

    Sartoris

    Joined:
    Dec 8, 2013
    Posts:
    21
    I wish to generate different types of terrain in each hexagon, but I'm not sure how to do it properly. Is there a way that you could suggest?

    Essentially, I want to create a piece of code that attaches a different material to each hexagon during the generation of the map, but I'm not sure what's the correct approach.
     
  10. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    Sorry this is not something I would be able to help with.

    But a simple method would be to set a random terrain type for a hex. Then for more complex terrains the neighboring hexes pick from a pool of compatible terrain types based on the previous hex. Say Hex 1 is a grassy hill terrain type. Hex 2 can be grassy planes, hill, or mountains, but not icy or desert .
     
  11. slugiscool99

    slugiscool99

    Joined:
    Mar 8, 2014
    Posts:
    2
    Hi @ForceX!

    This is awesome, great work! Just a quick question... I can't figure out how to place an object (when I try obj.transform.position = MouseHex.transform.position; it doesn't work...) Can you help me?

    Thanks

    Adam
     
  12. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    Hi Adam sorry for the delay in responding i haven't checked in on this thread in quite some time now. I take it you are attempting to access the MouseHex information from an outside script to move your object, correct?

    I made this simple example script that can be placed on any object and will position it to any clicked Hex. For this example you will want to remove the objects collider or setup some layer mask so the object collider does not interfere with the mouse raycast hex detection.

    Code (csharp):
    1.  
    2. //Add this script to a cube
    3. //You will need to make the MouseHex variable public to access it.
    4. var FXMM : FX_Map_Manager; // Link to the Map manager to access the Raycast MouseHex information.
    5.  
    6. function Update () {
    7.     if(Input.GetMouseButton(0)  FXMM.MouseHex.transform != null){
    8.         transform.position = FXMM.MouseHex.transform.position; // Place this object at the Hex's position.
    9.     }
    10. }
     
    Last edited: Mar 28, 2014
  13. Jamiska

    Jamiska

    Joined:
    Mar 13, 2015
    Posts:
    2
    Hi there,

    Noobie in GameDev here asking about FX Hex Grid. Is there a way to remove the camera option? Becuase i'd like to fiddle with it myself.
     
  14. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    Sorry I have not looked at this script in a very long time and don’t have access to unity ATM, but you should just be able to find the line that sets the camera position and comment it out. The camera's transform should be cached with the other variables at the top of the script. Find that and you can find where the camera's position is being set.

    Note that if you do this I would not recommend using the map wrapping feature. The line you are going to be looking for will be something like PlayerCameraT.position = somePosition; <-- This is just an example and not the actual line.
     
  15. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    If you still need assistance let me know and I can look into this when I get home tonight.
     
  16. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    Got home and took a quick look at the script. To quickly remove the camera you can edit the FX_Map_Manager.js and comment out or remove "function UpdateCameraW();" This will do away with any camera movement code.

    Ugg this code needs some serious lovin..
     
  17. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    Update 1.5.0

    Looking at the old v1.2.0(JS) code made me cringe. So I started over and rewrote everything as C#. The map generation received a huge cleanup.

    The old v1.2.0(JS) will stay around for legacy support. Starting with 1.5.0 everything will be compiled in Untiy 5.

    New features:
    1. Hex meshes are now procedurally generated.
    2.Hex orientation can be changed to have either the pointy part of the hex on top or the flat part of the hex on top.

    Changes:
    1. Completely rewritten in C#
    2. Modular scripting. Gives easy access to hex generator or map generator with out player controls getting in the way.

    Removed:
    1. Map Wrapping it didn't really work I may revisit it again later.
    2. Keep symmetrical. Will probably bring this back.

    Setup v1.5.0
    1. Create an empty gameObject and apply the scripts FX_Map_Gen.cs, FX_Hex_Gen.cs, & FX_Player.cs.
    2. Set a map width and height in the FX_Map_Gen inspector.
    3. Set the PlayerCamera in the FX_Player inspector
    4. Apply a material to the Material field in the FX_Hex_Gen inspector.
    5. Create a UI canvas and a Text element named "Distance Text"
    6. Run

    Enjoy


    Edit:
    Re-Upload file. Removed other projects getting c# conversions.
     
    Last edited: Mar 14, 2015
  18. Jamiska

    Jamiska

    Joined:
    Mar 13, 2015
    Posts:
    2
    Okay, yeah i got it with some fiddling now. Purely a Java man here so takes a time to get the jists. Thank you for your time though, you rock man.
     
  19. Popcony

    Popcony

    Joined:
    Apr 14, 2014
    Posts:
    17
    I love this project, but I was wondering, would It be possible to change the grid to be more "spherical", like 'hexplanet' I know there is a warping mechanic in the camera...
    Or, Would it be possible to do something like wrapping the grid around a UV sphere?
     
  20. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    With the current code, No. While cool this isn't something I plan on attempting. If this is a feature you are needing I would recommend getting hexplanet or feel free to modify the code to get the effect you desire.
     
  21. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    i wrote an endless distance landscape generator that can combine up to 100 parametric equasions for different kinds of mountains. it uses hex plane generator that i released on unity wiki createplane.
     
  22. cpage

    cpage

    Joined:
    May 20, 2015
    Posts:
    1
    I just checked out the demo project and this is quite awesome. Its exactly the foundation I was looking for. Thanks!