Game Development Reference
In-Depth Information
Declare the public variable numberWidth to hold the number of prefabs to be
placed across the width of the elevated track and assign an initial value of 8;
similarly, declare the public variable numberLength to hold the number of
prefabs to be placed down the length of the elevated track and assign an initial
value of 34.
3.
(4) for (var i : int = 0; i < numberLength; i++)
4.
This for loop iterates through the number of rows to be created.
(5) for (var j : int = 0; j < numberWidth; j++)
5.
This loop iterates through the number of prefabs to be instantiated for each
row.
(6) var position: Vector3 = Vector3(xPos, yPos, zPos);
6.
The x, y, and z coordinates are assigned to the variable position.
(7) Instantiate(prefab, position, Quaternion.identity);
7.
The new instance of the prefab is created at the assigned position and with
no rotation.
(8) xPos++;
8.
The x coordinate is incremented by 1 for each prefab created in this row. The
loop repeats, placing the next prefab alongside the previous one.
(9) xPos = -3.5;
9.
When a row of prefabs is complete, the x coordinate is reset to the initial
value for the next row.
(10) yPos = yPos + 0.05;
10.
The y coordinate is incremented by 0.05 for the next row.
(11) zPos = zPos + 1;
11.
The z coordinate is incremented by 2, placing the next row 2 units further
along the elevated track.
Save the script. Attach script to the Hazard Setup game object in the Hierarchy. With the
Hazard Setup game object selected in the Hierarchy, you can see in the Inspector that it has
both the RandomLandMinePositions script and the RegularLandMinePositions script attached
to it. Be sure to uncheck the RandomLandMinePositions script component to deactivate it; the
RegularLandMinePositions script should remain checked.
Search WWH ::




Custom Search