|
Hey guys. I'm at the end of my powers...I can't make one simple script :/. Simply what I need it to do, is to check, if there is no other block next it it, and if isn't instantiate. I'll post mine, it's really messy, because of trying to add dozens of vars, but it keeps instantiating even when there is one other. The algorithm should look like. If there is no block below instantiate a block below. If there is a block below that doesn't have tag "waterBlock" and if there aren't blocks on sides, instantiate blocks on sides. And repeat repeat repeat. I'm using InvokeRepeat because I wan't it to run like twice a second. And I'm using WaitForSeconds before the instantiating, so it isn't too fast. Yesterday it was working, but it was really fast, so I have put there a yield, and bam 5k cubes made in few secs :(. CLOSED -- David
(comments are locked)
|
var delay: float = 0.1;
var distance: float = 1.0; // distance between centers
private var pos : Vector3;
var prefab : Transform;
static var numBlock = 1;
var creator : Transform;
// this var is unique for all scripts
//static var busy = false; // this var is unique for all scripts
InvokeRepeating("CreateBlocks", .2, .5);
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()
{
if(creator == null)
{
Destroy(gameObject);
}
else
{
var hit:RaycastHit;
if(Physics.Raycast(transform.position, -transform.forward, hit, distance))
{
if(hit.collider.tag != "waterBlock")
{
if(!Physics.Raycast(transform.position, -transform.right, hit, 1))
{
DoBlock(-transform.right);
}
if(!Physics.Raycast(transform.position, transform.right, hit, 1))
{
DoBlock(transform.right);
}
if(!Physics.Raycast(transform.position, transform.up, hit, 1))
{
DoBlock(transform.up);
}
if(!Physics.Raycast(transform.position, -transform.up, hit, 1))
{
DoBlock(-transform.up);
}
}
}
else
{
DoBlock(-transform.forward);
}
}
}
function DoBlock(offset : Vector3)
{
var inst = Instantiate(prefab, pos + offset, transform.rotation);
inst.name = "Block"+numBlock;
inst.gameObject.GetComponent(testRewrited).creator = transform;
numBlock++;
}
(comments are locked)
|
|
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. Exactly that is my problem, that when I wanna slow down the creating or the blocks, so it not just like BAM and 125 object created, and than i run into this problem, that it sends command to instantiate, just before the another one...And nope, the framerate is pretty stable, because it creates the side blocks, just when there is a non water block below, and i have a energy system prepared, that will stops it from spreading to infinite at the sides..Any ideas, how should I make the code, so it will instantiate in a row, not instantly? PS: sorry for grammar, but I am rly tired.
Jul 10 '11 at 04:51 PM
Dávid Debnár
Ok, let me analyze this idea - I'll be back soon.
Jul 10 '11 at 05:18 PM
aldonaletto
Jul 10 '11 at 06:56 PM
Dávid Debnár
I edited my answer to include a replicating script. You must add this script to a single cube, and it will replicate itself one at a time while there's space available.
Jul 10 '11 at 07:35 PM
aldonaletto
thanks, ill try it
Jul 10 '11 at 07:43 PM
Dávid Debnár
(comments are locked)
|

have you considered using an array to handle all the logic instead of doing it through physics/raycasting? check out the questions on puzzle and match-3 games for examples of what I'm talking about.