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 craftymaniac · Oct 04, 2016 at 03:08 AM · beginnergridhexagon

Instantiating Hexagonal Cube coordinate help

Hey there, new to unity and C#. I have managed to create a rectangular hex grid but I need to create a hexagonal map of hexagons. Creating a rectangular grid starts in the bottom right corner, where a hexagonal shaped map starts at 0,0,0 which is what I am aiming for. Using Redblobgames. Looking at the pseudo code for hexagonal shape:

  unordered_set<Hex> map;
 for (int q = -map_radius; q <= map_radius; q++) {
     int r1 = max(-map_radius, -q - map_radius);
     int r2 = min(map_radius, -q + map_radius);
     for (int r = r1; r <= r2; r++) {
         map.insert(Hex(q, r, -q-r));
     }
 }

I'm having a hard time understanding what to do with

  int r1 = max(-map_radius, -q - map_radius);
  int r2 = min(map_radius, -q + map_radius);

It doesn't appear to be a method, but more like a Vector2, but I had troubles implementing that.

my current code:

 public class CubeHexGrid : MonoBehaviour {
 
 
     public GameObject HexPrefab;
     public int x = 5;
     public int y = 5;
 
     private float offsetX, offsetY;
 
     // Use this for initialization
     void Start()
     {
         Vector3 bounds = HexPrefab.GetComponent<MeshRenderer>().bounds.extents;
         float hexWidth = bounds.x * 2;
         float horzOffset = hexWidth * (3 / 4);
         float sqrt = Mathf.Sqrt(3) / 2;
         float hexHeight = sqrt * hexWidth;
         float vertOffset = hexHeight;
 
 
         offsetX = hexHeight * Mathf.Sqrt(3);
         offsetY = hexHeight * .5f;
 
 
         for (int i = 0; i < x; i++)
         {
             for (int j = 0; j < y; j++)
             {
                 Vector2 hexpos = HexOffset(i, j);
                 Vector3 pos = new Vector3(hexpos.x, hexpos.y, 0);
 
                 Instantiate(HexPrefab, pos, Quaternion.identity);
             }
         }
     }      
 
     Vector2 HexOffset(int x, int y)
     {
         Vector2 position = Vector2.zero;
 
         if (y % 2 == 0)
         {
             position.x = x * offsetX;
             position.y = y * offsetY;
         }
         else
         {
             position.x = (x + 0.5f) * offsetX;
             position.y = y * offsetY;
         }
         return position;
     }
 }

Any thoughts on that first bit of pseudocode to help figure out what it means?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by craftymaniac · Oct 05, 2016 at 05:26 AM

I've found the answer to this question.

  for (int q = -map_radius; q <= map_radius; q++) {
                 int r1 = Math.Max(-map_radius, -q - map_radius);
                 int r2 = Math.Min(map_radius, -q + map_radius);
                 for (int r = r1; r <= r2; r++) {
                     
                     
                     GameObject  myObject = Instantiate(hexPrefab, new Vector3(q,r,-q-r), Quaternion.identity)as GameObject;
                     myObject.transform.parent = transform;
                 }
             }


But now I am struggling to work with the lib that is provided on redblobgames libraries

I have copied the structs in and got rid of the errors by adding using System.Collections.Generic; which allowed for Math instead of Mathf(which doesn't work if you swap it out)

I'm not really sure how to use a struct like struct Point in the provided lib on redblobgames

 struct Point
 {
     public Point(double x, double y)
     {
         this.x = x;
         this.y = y;
     }
     public readonly double x;
     public readonly double y;
 }



Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can customize which prefabs are instantiated in a grid? 1 Answer

Hexagonal grid with 120 degree angles 1 Answer

Ground Plane when starting new project 2 Answers

Hexagonal grid 1 Answer

Change hex tilemap to use axial coordinates? 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