Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MathieuBlanchet · Jan 12, 2013 at 09:25 PM · randomspawngridhex

Random hex grid

Hey, I'd like to be able to make hexagon prefabs spawn randomly but next to each other, to make some sort of grid. That way, each time the game starts, the player would get a different grid. I really don't know how to approach this.

Any suggestions would be much appreciated, thanks!

Comment
Add comment · Show 3
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 robertbu · Jan 13, 2013 at 07:25 AM 0
Share

Does the grid shape change or does the contents of each hex square change? If the grid changes what is the shape. Is this grid going to be regular (i.e. some number of rows of the same count), random dense (every cell filled but wth a random shape), sparce (pathways of hex tiles), or something else?

avatar image MathieuBlanchet · Jan 13, 2013 at 03:50 PM 0
Share

ideally, it would not be a regular grid with a given number of row and they're would be different kinds of hexagons, but all the same shape.

avatar image robertbu · Jan 13, 2013 at 07:33 PM 0
Share

A picture would be very helpful. It’s hard to suggest a good approach without a better idea of what you are trying to do because there are lots of ways to solve this problem. For example you could produce a “full page” of hexagons and then, using whatever algorithm you wanted, change the texture for different looks or turn off the renderer and/or disable game objects to eli$$anonymous$$ate hexagons from the grid. You wouldn't be creating and destroying game object at runtime so there would never be any duplicates. Or you can start with a single hexagon and randomly walk outward creating objects as you go. To eli$$anonymous$$ate duplicates with this approach, keep an array of all the already created hexagons (or empty game objects if that is what you are using as placeholders). You can then compare the distance between all of the already placed hexagons and a proposed new hexagon. If the distance is below some small threshold, then the hexagon already exists. If you are going to be changing the grid frequently or had a very large number of hexagons, I might suggest yet another approach.

2 Replies

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

Answer by harko12 · Jan 13, 2013 at 06:46 AM

well, one thing you could do is make your prefabs, then spawn one, and do a random number between 1-6. Then add another hex to that side of the one you have. then random again, and add one to the new hex, and so forth. If you get a side that's already taken with the random number, then pick a different side, etc.

Pretty basic but it might get you going.

Comment
Add comment · Show 10 · 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 MathieuBlanchet · Jan 13, 2013 at 03:50 PM 0
Share

Thanks, I'll look into that! Although, how would you check if a hexagon is already on one of the sides? What I did is I used empty GameObjects as spawn points, and each time a number is randomly picked, a hexagon (with spawns attached to him as well) spawns at any of the spawns with the right position and rotation. But the problem now is the hexagons overlaying each other...

avatar image harko12 · Jan 13, 2013 at 08:32 PM 0
Share

After I gave the answer, I decided to try it out. I ran into the same problem. The simplest way would probably be to test if the spawn point overlaps an existing tile before spawning one. I had made a script for each tile that keeps track of it's neighbors on each side, but that may be overkill. If I get it working, I'll post the code

avatar image MathieuBlanchet · Jan 13, 2013 at 09:24 PM 0
Share

Sounds good, I'm trying to give the spawn points colliders or/and triggers to destroy themselves before a new tile is spawned, but OnCollisionEnter and OnTriggerEnter don't seem to be working in this case

Im also trying with Vector3.distance, but its not giving me the desired results either

avatar image tomekkie2 · Jan 13, 2013 at 10:51 PM 0
Share

Prior to the random number generation, you cast normal rays from all the sides to check which ones are actually empty.

avatar image harko12 · Jan 13, 2013 at 11:16 PM 0
Share

The Ray idea should work as long as the ray length is short enough not to see a tile more than one space away. probably simpler than a collider. The trick there will be getting the normal that you need. There may be an easy way to do that, but I'm not sure what it is.

Show more comments
avatar image
0

Answer by Chronos-L · Jan 13, 2013 at 07:13 AM

If you are looking for the same map, but different arrangements of grid, I will propose that you should manually make a generic map prefab (with a bunch of empty objects placed in a hexagonal patterns). Then on game start, you will randomly place different hexagons grid on each of these locations:

 public Hexagon[] hexs;   //Manully assigned
 public Transform[] arrangement; //Manually assigned
 
 private List <Hexagon> tiles = new List<Hexagon>();
 void Start() {
 
    for( int i = 0; i < arrangement.Length; ++i ) {
       Hexagon newHex = Instantiate( hexs[ Random.Range(0, hexs.length) ], arrangement[i].position, arrangement[i].rotation);
       tiles.Add(newHex); // This is optionally
    }
 }

Side note:

Assuming you have 1 blue tile, 1 red tile, and 1 green tile. And you need their ratio to be 7:2:1; in the inspector, you will put in 8 blue tile prefabs, 1 red and 1 green in the hexs variable.

For arrangement, you can either do it programmatically(can adapt to different map) or manually(cheap & quick solution)

Comment
Add comment · Show 2 · 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 MathieuBlanchet · Jan 13, 2013 at 03:53 PM 0
Share

Thanks, that's great, but I wouldn't want the same map everytime

avatar image Chronos-L · Jan 13, 2013 at 11:42 PM 0
Share

You can insert a null into the hexs for it to insert nothing as parts of the map, so you will get holes/missing tiles/blank tiles. But I'glad that you got your answer from harko12, I am not exactly sure of what you want, so I just provide a solution from what I can think of when I think about a random hex-grid map.

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

12 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

Related Questions

Drop item from airplane 1 Answer

AI spawning areas. What are the ways to make them? 1 Answer

Spawn object from Random Vector3 in array 0 Answers

Endless/infinite runner random enemy spawning multi lane 1 Answer

How to randomly replace the main platform with additional ones? 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