- Home /
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
Answer by Waz · Aug 07, 2011 at 10:25 PM
Use Random to leave some out.
I thought about doing that, but it would not make large enough gaps for my charter (1, 2, 1) to fit into...
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!
Sound like you want Perlin noise, not completely random noise. There are Answers here about Perlin noise.
another vote for perlin. this is how it's done in $$anonymous$$ecraft, which seems to be what you're trying to do.
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?