I’m running into a problem with an array where I can only check a condition (in this case, a randomly generated position) against a single element when I want to check against all the positions in the array.
For some context: every time the spawn function is called, I am going to instantiate two types of objects. The positions of both objects will be random, but must be at least a certain distance away from the other object and all instances of the Type_1 object that came before them. There will only be a single instance of the Type_2 object active at a time, but the Type_1 object will be limited to a set amount. When that maximum is hit, the oldest Type_1 object will be destroyed and a new object will be spawned.
Am I far off from doing what I want to (aside from the aforementioned problem)? I know that randomly checking positions could result in some wasted cycles, but because the total (I’m going to limit it to about 20-30), I don’t think it will cause any noticeable framerate issues.
Thanks.
var Collectable_Type_1: GameObject;
var Collectable_Type_2: GameObject;
var Type_1: GameObject[];
var MyPositions: Vector2[];
var Temporary: Vector2;
var Current: int;
var Placeholder: GameObject;
function Update(){
if(Input.GetKeyDown (KeyCode.Space))
Spawn();
}
function Spawn() {
Destroy(Placeholder);
if(Current <= MyPositions.Length){
for (var i : int = 0; i < MyPositions.length; i++){
Temporary.x = Random.Range(-5f,5f);
Temporary.y = Random.Range(-5f,5f);
if(Vector2.Distance(Temporary,MyPositions[i]) <= 1.5f){
i--;
}
}
MyPositions[Current] = Temporary;
var spawned_1 : GameObject = Instantiate(Collectable_Type_1, MyPositions[Current], transform.rotation);
Type_1[Current] = spawned_1;
for (var j : int = 0; j < MyPositions.length; j++){
Temporary.x = Random.Range(-5f,5f);
Temporary.y = Random.Range(-5f,5f);
if(Vector2.Distance(Temporary,MyPositions[j]) <= 1.5f){
j--;
}
}
MyPositions[Current] = Temporary;
var spawned_2 : GameObject = Instantiate(Collectable_Type_2, MyPositions[Current], transform.rotation);
Placeholder = spawned_2;
Current += 1;
}
if(Current == MyPositions.Length){
for (var k : int = 0; k < Type_1.length-1; i++){
Type_1[k-1] = Type_1[k];
}
Destroy(Type_1[Type_1.length]);
Current -= 1;
Spawn();
}
}