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 Tylia · Feb 17, 2017 at 06:32 PM · 3drandomprocedural generation

Problem of procedural generation using randomness

I have been thinking about this problem for a while but I can't manage to find where my algorithm is wrong :

I'm working on a procedural generated space game and I want the world to be composed of tiles that would have different functions, the problem is that I want them to be spaced so that the player can go between them, so I made so that they won't spawn next to each other but when I try to add tiles that are empty, the algorithm is kind acting weird :

Here is my code : using UnityEngine; using System.Collections;

 public class ChunkManagerS : MonoBehaviour 
 {
     public int chunkLength;
     public int spaceRange;
     public int randomness;
     public GameObject [,] primarChunk;
     public GameObject platformPrefab = null;
 
     void Start ()
     {
         primarChunk = new GameObject[chunkLength,chunkLength];
 
         for(int x = 0; x < chunkLength; x++)
         {
             for(int z = 0; z < chunkLength; z++)
             {
                 bool c = false;
 
                 for(int a = x - spaceRange; a <= x + spaceRange; a++)
                 {
                     for(int b = z - spaceRange; b <= z + spaceRange; b++)
                     {
                         if(a >= 0 && b >= 0 && a <= x && b <= z)
                         {
                             if(primarChunk[a,b] != null)
                                 c = true;
                         }
                     }
                 }
 
                 if(!c)
                 {
                     //if(Random.Range(0,100) < randomness)  Randomness system added later
                     //{
                         primarChunk[x,z] = (GameObject) Instantiate(platformPrefab, new Vector3(x, 0, z), Quaternion.identity);
                     //}
                 }
             }
         }
     }
 }

So everything is working well when I don't use randomness, for example if I ask for a 100 tiles chunk with a spaceRange of 2 I get this result : alt text

But if I had add the fact that a tile has 50% of chance to be empty, strange stuff happens : alt text

Some tiles' corners start colliding with other tiles' corner and it's not the result that I was looking for so if you have any ideas to resolve this problem I would really appreciate ^^.

Thx in advance !

capture.jpg (134.7 kB)
capture2.jpg (133.6 kB)
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
Best Answer

Answer by Tylia · Feb 17, 2017 at 09:01 PM

Finally got a way to go trough this problem and it's quite simple !

I just took a look at my 2 first for loops and realized that I could not just increment my x and z variables but that I could make them interact with other variables so I did this :

         for(int x = 0; x <= chunkLength; x += spaceRange)
         {
             for(int z = 0; z <= chunkLength; z += spaceRange)
             {
                 if(Random.Range(0,100) < randomness)
                 {
                     primarChunk[x,z] = (GameObject) Instantiate(platformPrefab, new Vector3(x, 0, z), Quaternion.identity);
                 }
             }
         }

Now my tiles don't check anymore if tiles around them are empty or not, they just check if they need to be empty or not using a random number ^^ The 2 for loops handle the format and the position of every tiles now to make it simple.

I still don't know where my algorithm was wrong but I got another way so I'll just say that this post is answered.

Thanx for all the answers thought ! ^^

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 AurimasBlazulionis · Feb 17, 2017 at 06:57 PM

Your for loops seem to be a complete nonsense, to be honest. You can easily fix them by simply doing the first 2 loops

 for(int x = 0; x < chunkLength; x++)
              for(int z = 0; z < chunkLength; z++)

Inside you can simply use this check:

 if (primarChunk[a, b] == null && Random.value < randomness)
        primarChunk[x,z] = (GameObject) Instantiate(platformPrefab, new Vector3(x*Random.Range(0,spaceRange+1), 0, z*Random.Range(0, spaceRange+1), Quaternion.identity);

This should check everything and decide if you need to spawn it. No need to put brackets. Also, for this, the randomness definition should look like this:

 [Range(0,1)]
 public float randomness;

It will have a nice slider.

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 Tylia · Feb 17, 2017 at 08:52 PM 0
Share

Thx @TheDiamondPlay for the answer but it is not helping me in my goal ^^' :

First I can't just say :

  if(primarChunk[a,b] != null && Random.value < randomness)
      primarChunk[x,z] = (GameObject) Instantiate(platformPrefab, new Vector3(x*Random.Range(0,spaceRange+1), 0, z*Random.Range(0, spaceRange+1), Quaternion.identity);

Because this will mean that I dont check tiles around the selected tile, also using randomness will make the tile appears at a position not handled by the grid that I tried to set using a 2dimensionnal array (the primarChunk array one) so thank you for the tip about shortening my for loops but I still don't know why my algorithm is making those strange patterns when I use randomness ^^

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

99 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

Related Questions

Randomly place objects USING ALPHA MAP 0 Answers

Trying to implement xorshift with deplorable results (PRNG) 0 Answers

how to add sprites name in code ? 0 Answers

Random Generation problem GameObject 0 Answers

Instantiating Objects randomly on a sphere with a set distance radius 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