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 /
  • Help Room /
avatar image
0
Question by Spudly1701 · Dec 21, 2017 at 03:33 PM · scripting probleminstantiategridspherehexagon

Instantiate GameObjects in a Sphere shape

Hi all,

I'm trying to figure out a way to Instantiate a bunch of GameObjects ('flat' hexagons) to create a sphere. Essentially a spherical TileMap.

Is this actually possible and would anyone be able to point me in the right direction for a solution?

Thanks Spud

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DreadKyller · Dec 22, 2017 at 01:38 AM

If you know the radius of the hexagon you can calculate everything needed:

It's important to remember that in a regular hexagon the length of each side is always equal to the radius. We need to envision the hexagon oriented this way: alt text Where the blue line is the radius:

The height is then equal to radius*2
The width is then equal to radius*sqrt(3)

Every odd numbered layer will be horizontally offset by width/2
Every even numbered layer will not be horizntally offset.
Every layer will have a vertical offset of height*0.75

Now we'll go through a 2D loop:

 float ySpacing = height * 0.75f;
 float xStagger = width / 2f;

 int xCount = Mathf.CeilToInt(circleRadius / width);
 int yCount = Mathf.CeilToInt(circleRadius / ySpacing);

 float circleRadiusSqr = circleRadius * circleRadius;

 // In cases where x is staggered by y position additional x unit is needed
 // Thus the -1
 for (int x = -xCount - 1; x <= xCount; x++) {
     for (int y = -yCount; y <= yCount; y++) {
         float yOffset = y * ySpacing;
         float xOffset = x * width + ((y % 2) * xStagger);
 
         if (xOffset * xOffset + yOffset * yOffset <= circleRadiusSqr)
         {
             Vector3 position = origin + xAxis * xOffset;
             position = position + yAxis * yOffset;
 
             // Do instantiation using position vector, replace xAxis and yAxis with any vector
             // Pointing in that axis, standard axis use the axis defined
             // in Vector3 (such as Vector3.up) or your own. If creating a 2D scene, use Vector2
         }
     }
 }

If you need a sphere, not just a circle repeat this multiple times with a different origin on the axis you don't modify in the loop above. The new radius at any given hgieht in a sphere can be obtained with: sqrt(circleRadiusSqr - sampleHeight * sampleHeight) Where sampleHeight is the height of a sphere where you wish to get the horizontal radius, equal to getting the width of a circle at a specific height.

Hope this was clear enough to help.


hexagon-radius.png (6.9 kB)
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
avatar image
0

Answer by Spudly1701 · Dec 22, 2017 at 03:57 AM

I've not had a chance to try this out yet, but I think I understand the thinking. Excellent explanation, thank you, really appreciate it.

So, to see if I have it right in my head.....

Define width/height (based on hex width) Define circle radius (for a ring of hexes)

For a circle use the equation in the last paragraph with a 'new' sampleHeight (currentHeight + radius*2) - simple example and repeat for amount of 'rows' needed to create sphere.

Assuming we start at sampleHeight=0, 2 loops, 1 for +y sampleHeight, 1 for -y sampleHeight ?

Spud

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

188 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate a Prefab along the surface of a sphere. C# 0 Answers

Hexagon grid check for specific tile 0 Answers

Assign variables on Instantiation? 0 Answers

Add Component with Script Data 1 Answer

How to change the Bool for only a single prefab GameObject creating with Instantiate? 2 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