Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 AVividLight · Aug 07, 2011 at 10:22 PM · instantiaterandomgridcube

Adding Imperfections?

Hey guys,

Sorry for the non-descriptive title, but I couldn't think of what to call this. What I have here is a home-made code to make a large cube out of smaller cubes. What I want to do is have it load the cube, but with random "gaps", or missing cubes. Similar to THIS, but without the red and blue stuff... Following is the code I wrote that "loads" the cubes. If anyone can give me some advice, or give me a start, I will be grateful!

 using UnityEngine;
 using System.Collections;
 //Written By Gibson Bethke ][ GBStudios
 public class LoadQuadrateCode : MonoBehaviour {
     public GameObject blockPrefab;
     public int blockSize = 3;
     public int rowSize = 7;
     public float loadingSpeed = 0.05F;
     public bool doneLoading = false;
     
     int iUp;
     int iToTheRight;
     int iBack;
     int upRowSize;
     int actualRowSize;
     
     Vector3 initialPosition;
         
     void Start () {
         actualRowSize = rowSize -1;
         initialPosition = gameObject.transform.position;
         InvokeRepeating("Load", 1, loadingSpeed);
     }
     
     void Load () {
         if(iUp == rowSize && iToTheRight == actualRowSize && iBack == actualRowSize) {
             doneLoading = true;
             CancelInvoke();
         }
         if(iUp == rowSize && iToTheRight < actualRowSize){
                 iUp = 0;
                 iToTheRight ++;
         }
         if(iUp == rowSize && iToTheRight == actualRowSize && iBack < actualRowSize) {
             iBack ++;
             iUp = 0;
             iToTheRight = 0;
         }
         if(iUp < rowSize) {
         Instantiate(blockPrefab, initialPosition + new Vector3(iToTheRight * blockSize, iUp * blockSize, iBack * blockSize), Quaternion.identity);
             iUp ++;
         }
     }
 }

Thanks for your attention, and help guys!

-Gibson


-Edit:

I have implemented Random.Value like Warwick Allison said, and it is working fairly well; is it possible to make it more predictable? The following is my update code.

 using UnityEngine;
 using System.Collections;
 //Written By Gibson Bethke ][ GBStudios
 public class LoadRandomQuadrateCode : MonoBehaviour {
     public GameObject blockPrefab;
     public int blockSize = 3;
     public int rowSize = 7;
     public float loadingSpeed = 0.05F;
     public bool doneLoading = false;
     public float amountOfRandom = 0.5F;
     
     int iUp;
     int iToTheRight;
     int iBack;
     int upRowSize;
     int actualRowSize;
     
     float randomNumber;
     
     Vector3 initialPosition;
 
     void Start () {
         actualRowSize = rowSize -1;
         initialPosition = gameObject.transform.position;
         InvokeRepeating("Load", 1, loadingSpeed);
     }
     
     void Load () {
         randomNumber = Random.value;
         if(iUp == rowSize && iToTheRight == actualRowSize && iBack == actualRowSize) {
             doneLoading = true;
             CancelInvoke();
         }
         if(iUp == rowSize && iToTheRight < actualRowSize){
                 iUp = 0;
                 iToTheRight ++;
         }
         if(iUp == rowSize && iToTheRight == actualRowSize && iBack < actualRowSize) {
             iBack ++;
             iUp = 0;
             iToTheRight = 0;
         }
         if(iUp < rowSize) {
             iUp ++;
             if(randomNumber < amountOfRandom) {
                 Instantiate(blockPrefab, initialPosition + new Vector3(iToTheRight * blockSize, iUp * blockSize, iBack * blockSize), Quaternion.identity);
             }
         }
     }
 }

Again, thanks for the help, guys!

-Gibson

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
1
Best Answer

Answer by Waz · Aug 07, 2011 at 10:25 PM

Use Random to leave some out.

Comment
Add comment · Show 6 · 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 AVividLight · Aug 07, 2011 at 10:32 PM 0
Share

I thought about doing that, but it would not make large enough gaps for my charter (1, 2, 1) to fit into...

avatar image AVividLight · Aug 07, 2011 at 11:00 PM 0
Share

I ended up implementing it, and it works fairly well, but I would like to make it a little more predictable... Anyway, thanks for the tip!

avatar image Waz · Aug 08, 2011 at 12:35 AM 2
Share

Sound like you want Perlin noise, not completely random noise. There are Answers here about Perlin noise.

avatar image testure · Aug 08, 2011 at 12:58 AM 1
Share

another vote for perlin. this is how it's done in $$anonymous$$ecraft, which seems to be what you're trying to do.

avatar image AVividLight · Aug 08, 2011 at 01:28 AM 0
Share

Do you guys have any tips about how (or where) to learn Perlin Noise? There is nothing on the Scripting Reference, and that is where I usually look for information... that and $$anonymous$$SDN...

-Edit

I can't find anything on $$anonymous$$SDN either... You guys have any idea how I could learn?

Show more comments

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 to instantiate a prefab at mouse pos 1 Answer

Creating a grid of cubes. 1 Answer

How to access property of a prefab before Instantiating. 1 Answer

Make sure objects are not instantiated on each other 2 Answers

C# Spawn Random Object, at Random 2D Location 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