This script creates first a clone of itself in the forward direction; in the next step, it creates four clones at left, right, up and down. But each clone instantiated generates its own 5 clones (of less if blocked at any side), and these clones will generate others, and so on. If a few frames, your world will be full of replicating clones - and your frame rate will drop to zero. Another thing: you should check for neighbours and create the new blocks in the same frame, or other clones will not “see” these newcomers and instantiate others at the same positions.
EDITED: This script will replicate the original block with delay interval between each block replication. The replications will be restricted by external colliders. They replicate in all directions, but you can eliminate the unwanted ones.
This is a very tricky script: it uses the static variable busy to ensure that only one block will replicate at a time. At Update, when a block notices busy is false, it sets it to true and starts its own replication. A replication takes several frames, since a delay is started each time a side clone is created. Since busy is set, no other block will replicate until this one finishes - at the end of the replication, it clears busy . Once replicated, the block sets the replicated var, signalling to itself that it was already replicated. You can check the static var busy to know when all blocks replicated: if it’s false for two consecutive Updates, there are no more blocks to replicate.
var delay: float = 0.1;
var distance: float = 1.1; // distance between centers
private var pos : Vector3;
private var replicated = false; // replicates only once
static var numBlock = 1; // this var is unique for all scripts
static var busy = false; // this var is unique for all scripts
function Start(){
pos = transform.position;
}
function Update(){
if (replicated) return; // only replicates once
if (!busy){ // if nobody is replicating now
CreateBlocks(); // do the replication
}
}
function CreateBlocks(){
var hit:RaycastHit;
if(!Physics.Raycast(transform.position, transform.forward, hit, distance)){
DoBlock(transform.forward);
}
if(!Physics.Raycast(transform.position, -transform.right, hit, 1)){
DoBlock(-transform.right);
yield WaitForSeconds(delay);
}
if(!Physics.Raycast(transform.position, transform.right, hit, 1)){
DoBlock(transform.right);
yield WaitForSeconds(delay);
}
if(!Physics.Raycast(transform.position, -transform.forward, hit, 1)){
DoBlock(-transform.forward);
yield WaitForSeconds(delay);
}
if(!Physics.Raycast(transform.position, transform.up, hit, 1)){
DoBlock(transform.up);
yield WaitForSeconds(delay);
}
if(!Physics.Raycast(transform.position, -transform.up, hit, 1)){
DoBlock(-transform.up);
yield WaitForSeconds(delay);
}
replicated = true; // this block's replication is done
busy = false; // enable others to replicate
}
function DoBlock(offset : Vector3){
var inst = Instantiate(gameObject, pos + offset, transform.rotation);
inst.name = "Block"+numBlock;
numBlock++;
busy = true;
}