Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ParagonTime · Oct 04, 2016 at 08:44 PM · mathmaphexagon

Need a bit of help with some math in regards to hex nodes

After reading a fair amount on how to create hex grid based maps I decided to try my hand at creating a simple 2d map with just the hex tiles. I found that in order to facilitate the squiggly pattern needed for a hex map that the easiest (at least to me) is to add and subtract from one of the variables every time it generates a tile in whatever pattern needed(depending on if its a flat topped or pointy topped hex).

I'm using flat topped meaning that my Y axis is the same as in a square grid but my X axis has to squiggle in order to fit correctly this is what the below code is supposed to do:

 void Start () {
         for(y = 0; y < height; y = y + 0.88f){ 
             for(x = 0; x < width; x = x + 0.769f ){ 
 
                 y = y + 0.451f; // this is supposed to work with the below y = y - 0.451f to increase, generate then revert to the y axis actual value
 
                 Vector3 spawnPoint = new Vector3(x, y, 0); //this code and the next two lines are what generate the hex's according to an array elsewhere
                 var groundNode = (GameObject)Instantiate (hexGridTiles [Random.Range (0, hexGridTiles.Length)], spawnPoint, Quaternion.identity);
                 groundNode.transform.SetParent(transform, true);
 
                 y = y - 0.451f;
             }
         
         }
 
 
 
     }
     


Instead the hex's attempt to generate in a square grid fashion- heres a screenshot of whats happening:

alt text

The grids are stating with a Y pos of 0.451 and are not adding nor subtracting when moving on the X axis. They are also adding both Y values to the Y axis when it generates upwards meaning the first y grid after origin instead of being at 0.88 is sitting on 1.331.

Yet when I manually change the values to what they should be it looks like thisalt text

I'm either doing my math or my programming wrong but for the life of me I cant see where the mistake is. Any help/future tips on this, would be much appreciated.

capture1.png (55.9 kB)
capture2.png (41.8 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image doublemax · Oct 04, 2016 at 09:30 PM 0
Share

This tutorial might help:

http://catlikecoding.com/unity/tutorials/hex-map-1/

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bonfire-Boy · Oct 04, 2016 at 09:26 PM

It looks like you're offsetting the y value for every column. Don't you only want to do it for alternate columns?

I think something like this would do...

 void Start () 
 {
     for (y = 0; y < height; y = y + 0.88f)
     {
         bool doOffset = false; 
         for(x = 0; x < width; x = x + 0.769f )
         {
             float offset = doOffset ? 0.451f : 0f; 
             y = y + offset; 
             Vector3 spawnPoint = new Vector3(x, y, 0); 
             var groundNode = (GameObject)Instantiate (hexGridTiles [Random.Range (0, hexGridTiles.Length)], spawnPoint, Quaternion.identity);
             groundNode.transform.SetParent(transform, true);
  
             y = y - offset;
             doOffset = !doOffset;
         }
     }
 }
      
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ParagonTime · Oct 04, 2016 at 11:32 PM 0
Share

Yup this pretty much did it. Thank you I knew it was something simple I was missing.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Interactive Hexagon Map Errors 0 Answers

Saving a Triple Byte Array [,,] 0 Answers

Creating an UI GPS map 0 Answers

Best practices for interactive hexagon map? 1 Answer

Assigning UV Map to model at runtime 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges