Hi. I want to spawn objects in an organization pattern that is just like this, but with about 15 rows (this translates to 100+ individual objects…making manual placement very impractical and tedious).
The structure is as follows: the number of objects in a row is equal to the number of the row (starting from the front) and the first object in each row (starting on the left) is offset. The objects then have twice the size of the object between them (along the x axis). There is also 1 unity of space between each row (on the y axis).Following the image I posted, object 1 would be at (0,1,0), object 2 would be at (-1,1,0), 3=(1,1,0), etc.
The image does a much better of explaining it than me.
Anyways, I tried setting up my code. The “Total_Rows” variable is the number of rows I want. In my tests, it was set to three. It spawns the first object at (0,1,0)…but then the code stops functioning. I am not getting any errors, but nothing happens. What did I do wrong? Thanks
var Total_Rows : int;
var Current_Row: int;
var Current_Box: int;
var BOX : GameObject;
function Start(){
Invoke("Spawn",0);}
function Spawn(){
if(Current_Row<=Total_Rows){
if(Current_Box<=Current_Row){
Instantiate(BOX, Vector3((Current_Row*-1)+(Current_Box*2),Current_Row,0), transform.rotation);Current_Box+=1;}
if(Current_Box==Current_Row){
Current_Row+=1;Current_Box=1;}
Invoke("Spawn",.1f);
}
}
2 Answers
2
Your Code is correct. However it needs only 1 modification.
You need to change this line →
if(Current_Box==Current_Row)
to this →
if(Current_Box > Current_Row)
Here is the modification that I got working .
var Total_Rows : int;
var Current_Row: int; // use 1 here
var Current_Box: int; // use 1 here
var BOX : GameObject;
function Start(){
Invoke("Spawn",0);}
function Spawn(){
if(Current_Row<=Total_Rows){
if(Current_Box<=Current_Row){
Instantiate(BOX, Vector3((Current_Row*-1)+(Current_Box*2),Current_Row,0), transform.rotation);
Current_Box+=1;
}
if(Current_Box > Current_Row)
{
Current_Row++;
Current_Box = 1;
}
Invoke("Spawn",.1f);
Debug.Log("Rows Left");
}
Debug.Log(Current_Row + "" + Current_Box);
}`
Just use the initial values of current box and current row as 1. Since the 0 row will have 0 Boxes.
Hope it helps.
Maybe not exactly what you want, but try with a double lerp between three points, like :
var A : Transform;
var B : Transform;
var C : Transform;
var prefab : GameObject;
var row : float = 5f;
function OnDrawGizmos()
{
Gizmos.DrawSphere( A.position, 0.5 );
Gizmos.DrawSphere( B.position, 0.5 );
Gizmos.DrawSphere( C.position, 0.5 );
}
function Start ()
{
for( var i = 0; i < row; i++){
var _if : float = i;
var ti : float = _if / (row-1);
for( var j = 0; j <= i; j++){
var _jf : float = j;
var tj : float = i == 0 ? 0f : _jf / _if;
Instantiate( prefab, Vector3.Lerp( B.position, Vector3.Lerp( A.position, C.position, tj ), ti ), Quaternion.identity );
}
}
}
PS : I’m not familiar with javascript, so be carefull.
PS2 : I doubt performance is an issue here, but Eric5h5 did lerps functions without clamps (so faster) and you can find them in the wiki. Just so you know.
Thank you :)
– Tofudude624