Unity Community


Page 1 of 4 123 ... LastLast
Results 1 to 20 of 62

  1. Location
    Schertz, Texas
    Posts
    838

    FX Hex Grid (Hex Grid Generator)



    FX Hex Grid will generate a Hex based map using a base Hex geometry and an input for Map Height & Width. Each Hex section will have a script component that contains its location in the map. Top left is 1,1.

    This is very basic and is intended as a starting point for Hex based map generation. Modify it to fit your game. Enjoy.

    Current Build Version 1.2.0

    New Features:
    1. Hex Map Wrapping - By enabling "Enable Wrapping" you will be able to create a hex map that seams to wrap around its x axis.
    2. Wrapped Hex Map Counting - when "Enable Wrapping" is enabled you will all ways be able to find out how far you are from a desired hex even when wrapping from left to right or right to left.
    3. Able to set initial camera height.
    4. Able to set initial camera angle.

    Changes:
    1. Keep Symmetrical will be set to disabled if Enable Wrapping is enabled.

    Setup
    1. Create an empty gameObject and apply the script FX_HexMapGen.js
    2. Apply the Hex prefab to the FX_HexMapGen --> Hex Grid
    3. Set the FX_HexMapGen --> Map Width & Height
    4. Run

    Tip:
    1. For non joining maps enable Keep Symetrical. This will keep the edges of the map the same.
    2. While in the unity editor generating a large map 200 x 100 will take a minute to compute. Outside unity in a compiled state this is much faster.

    WebPlayer DEMO
    This demo shows a grid generated at a size of 20x10 with Keep Symmetrical enabled.




    HOT FIX

    Edit the FX_Map_Manager.js and replace the function CalculateDistance() with :

    Code:  
    1. function CalculateDistance(){
    2.  
    3. var dx : int = Mathf.Abs(GoToHex.x - CurrentHex.x);
    4. var dy : int = Mathf.Abs(GoToHex.y - CurrentHex.y);
    5. var dz : int = Mathf.Abs(GoToHex.z - CurrentHex.z);
    6.  
    7. var DistA : int = Mathf.Max(dx, dy, dz);
    8. var DistB : int = Mathf.Abs(DistA - Mathf.Abs(MapSize.x + dy));
    9.  
    10.     if(EnableWrapping){
    11.         if(dy > DistB){
    12.              Distance = Mathf.Min(DistA, dy);
    13.         }else{
    14.              Distance = Mathf.Min(DistA, DistB);
    15.         }
    16.     }else{
    17.         Distance = DistA;
    18.     }
    19. }


    Internet Explorer 9 users. If you are having problems downloading files from the fourm please enable Compatibility View
    Attached Files


  2. Posts
    38
    This is pretty awesome. Good work!


  3. Location
    Schertz, Texas
    Posts
    838
    Thanks . The next step is to address the hex grid to an array so it can be accessed by other elements such as an "Ai". For those who know more than I; should have no problem with this. Right now I'm kinda making it up as I go along.


  4. Location
    Schertz, Texas
    Posts
    838
    New Build 1.1.0

    New Features
    1. Distance counting : This new implementation will find the distance from any one Hex to any other Hex on the map.

    Changes:
    1. Changed the way hexes are numbered. New system uses a vector3 instead of a vector2.
    2. Hex 0,0,0 is now the bottom left

    Updated WebPlayer DEMO

    You can click on a Hex to set it as your current position hex and then move the mouse over other hexes to see the distance to that hex. Your starting hex is 0,0,0.

    Phew.. Creating that distance counter was a new experience, & i learned quite a bit about how to use some Mathf functions that i haven't had the opportunity to use before; even if they are not the ones i ended up using in the code. Much fun was had.


  5. Posts
    281
    The vector3 numbering scheme is a bit confusing. Is there a reason you moved from a 2D system?


  6. Location
    Schertz, Texas
    Posts
    838
    @ g00niebird : Yes. The Vector3 makes distance calculations very easy as it's now the Max of the Abs value of the difference of x -x or y - y or z - z.
    The z you will notice is just a the negative value of x + y with in that cell. X & Y are still your position within the grid. Z is only used for distance calculations. I'll post up a new image demonstrating this a bit later, but i'm off to work right now. You can check out this PAGE for more info.


  7. Location
    Deep in the heart of Texas
    Posts
    280
    Have you seen my HexTech framework?
    Tasarran
    ----------
    Under Steel Skies - War with custom-built robots in the far future
    HexTech - Hexagon tile map framework - now on the Asset Store!
    Take a test drive at www.electricrune.com


  8. Location
    Schertz, Texas
    Posts
    838
    I have. This project is more for my experience in scripting than any thing else.

    Next update: (This weekend)
    1. Camera will scroll around the map creating a wrapping effect.
    2. Counting across a wrapped map.


  9. Location
    Schertz, Texas
    Posts
    838
    New Build 1.2.0

    New Features:
    1. Hex Map Wrapping - By enabling "Enable Wrapping" you will be able to create a hex map that seams to wrap around its x axis.
    2. Wrapped Hex Map Counting - when "Enable Wrapping" is enabled you will all ways be able to find out how far you are from a desired hex even when wrapping from left to right or right to left.
    3. Able to set initial camera height.
    4. Able to set initial camera angle.

    Changes:
    1. Keep Symmetrical will be set to disabled if Enable Wrapping is enabled.

    Known issues:
    1. For some reason the camera seams a bit jerky at times. I'm not sure why atm.

    Updated WebPlayer DEMO to latest build.

    This is a first attempt at creating a wrapping world. It's not perfect but it's close enough for a release. This is something i will continue to work on.


    Previous Updates

    Version 1.1.0
    New Features
    1. Distance counting : This new implementation will find the distance from any one Hex to any other Hex on the map.

    Changes:
    1. Changed the way hexes are numbered. New system uses a vector3 instead of a vector2.
    2. Hex 0,0,0 is now the bottom left

    Version 1.0.3
    Changes:
    1.Changed the orientation of the Hexagon. This makes counting much easier and the map neater.


    Version 1.0.2
    New Features:
    1. Keep Symmetrical. This option will keep the edges of the map symmetrical.

    Version 1.0.1a
    New Features:
    1. Added simple mouse hex highlight interaction.
    2. Added hex tile template texture.

    Fixes:
    1. Corrected an issue with row / column counting.


  10. Posts
    2
    This is really great, thanks so much for sharing it!

    I'd like to use this in my first Unity project, a hex-based Risk clone. Would I be able to alter the scripts to allow me to manually place hexes in a grid (via the editor)? I'm still catching up on Unity-based development.


  11. Location
    Schertz, Texas
    Posts
    838
    Yes. You can modify the scripts as you see fit for your game.


  12. Posts
    99
    is it possible to elevate hex blocks?
    any chances for AI for this thing?
    No Image BBCode - NO SIGNATURE FOR YOU


  13. Location
    Schertz, Texas
    Posts
    838
    @nagata: I don't see why you couldn't elevate the hex. Though the hex is just a flat plane, there would be nothing under it. You would have to either bring in your own 3D hex and adjust the vertices for your height. Or make some type of mask to cover up the empty space below the hex. I'm sure there are lots of ways.

    As for Ai. Not any time soon. I only created the hex board as an experiment. Mainly to understand how to count hexes. The math to get from one hex to the next is in the code so it should be able to be re-purposed for an Ai. As long as the Ai knows which hex it's on and what hex it wants to get to. For that you would want to create a table of the hexes that contains its position, land type, and any resources. Then the Ai could look at the table and say "i want to get to hex blah, blah". You would also want a movement counter to check how many more moves you have.


  14. Posts
    39
    Hey, excellent work! I prefer this coordinate system for hexes, as it makes things like pathfinding (which I've added to this easily!) much simpler.

    One question though, assuming you're still working on this: do you have any suggestions for converting world coordinates (say from a camera->ground plane intersection) to hex coordinates? I'm interested in procedurally generating the hex meshes, or combining the meshes after instantiation and removing the gameobjects/colliders that are currently used for each hex. Having a GO/collider for tens of thousand of hexes just won't work without a paging/viewport system of some kind, but I'm stuck trying to figure out a formula to do mouse picking without the colliders.

    Thanks for any help, would love to see more updates to this project!


  15. Location
    Schertz, Texas
    Posts
    838
    @Mingo: I see exactly what your want to do but unfortunately i don't have an answer for you. I do seam to remember an article i read on the internet while i was searching for a method of counting across hexes. I think the article was using a pixel color look-up for hex position information from a flat plain / texture. I only think that is what the article was about. I didn't spend too much time reading it, i only thought it was interesting but out of my realm.

    One thing you could try would be a way to count to the center of a hex from wold space. If the center of a hex to any other hex is 1 unit then you could convert your X,Y world position to some Hex position.

    It would look something like:
    World position X=4 Y=0
    This would be the fifth Hex to the right of Hex 0,0.
    Hex Space = 4,0

    You would just need to figure out your vertical offset.
    You know that your first row of hexes is starting at 0,0. This makes the first row an even number. Then your second row 0,1 is an Odd number. Your odd row Hexes are off set by from the row above or below them by .5. So you would just need to check if your Y position is odd or even then adjust your Hex count based on this.

    It would look something like:
    World position X=4.5 Y=2
    This would be the fifth Hex to the right on the third row.
    Hex Space = 4,2

    Remember you are counting starting with 0. Visually 0 + 4 would be the fifth hex, but in an array it's stored as 4. Remember your array offset for a visual conversion.

    This is far from perfect as you would loose accuracy as you get closer to the edge of a hex.

    This is just an idea that with some work might be able to work. What ever you choose to do let me know if you get it to work the way you want. I would like to know. GL


  16. Posts
    39
    There's probably a much simpler way to translate from hex to world coords and back, but the approach I'm taking for now is to plot a line in world space from hex[0,0] to hex[0, maxY] to simulate the hex Y axis. I then find the closest point on this line to the mouse cursor's world position. This gives me the Y coordinate in hex space. The distance of the point from the line gives me the hex X coordinate.

    http://www.topcoder.com/tc?d1=tutori...&module=Static for more info on it, especially "Line-Point Distance" algorithm on the page.


  17. Posts
    13
    Could you explain in full how to apply this thing? Your setup isn't very clear on how to get the grid work. All I've got is one hex and when I try to get the game to play to test the hex out the game just keeps pausing. I can't find FX_Hex_Map_Gen or whatever.


  18. Location
    Schertz, Texas
    Posts
    838
    Quote Originally Posted by MoPMetallica View Post
    Could you explain in full how to apply this thing? Your setup isn't very clear on how to get the grid work. All I've got is one hex and when I try to get the game to play to test the hex out the game just keeps pausing. I can't find FX_Hex_Map_Gen or whatever.
    Easy

    1. Create a new empty GameObject named "_MapMgr"
    2. Assign the script FX_Map_Manager to "_MapMgr"
    3. In the inspector assign:
    ....a : Cam 1- Is a camera
    ....b : Hex Grid - Is a mesh / prefab

    4. Set a value for Camera Height, Camera Angle, Camera Speed. Demo uses 3, 65, 2
    5. Set your Map Size.
    6. Run


  19. Posts
    1
    This is amazing. Thanks for the great work. I only just started working with unity, but this will help a lot. Cheers.

    Quick question tho. Following just the instructions above and setting my Map size to 10,10 the distance calculation does not remain accurate. It seems to assume that Setting the active tile to 0,0,0 then moving your mouse across to 5,0,-5 measures a distance of 5, but moving any further across lowers the distance until at 9,0,-9 it assumes the distance is 1. It's like it is assuming it can wrap back around from 9,0,-9 to 0,0,0. I can get around that by simply creating my own distance measurement method, but I was just wondering why your default behavior acted in this way. (Note: I have Enable Wrapping and Keep Symmetrical both off).
    Last edited by Bluevein; 10-25-2012 at 07:12 PM.


  20. Location
    Egypt <-> China
    Posts
    125
    intersting tool as it becomes so important for lots of games i guess we will forget the square tiles soon i`ll miss them :P

    m;
    what is better than making a game !?
    Check My Games !

Page 1 of 4 123 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •