Game Development Reference
In-Depth Information
What if you don't want a random arrangement? Besides manually placing the prefabs, you can also
use scripts to place hazards in regular patterns. In the Project panel, open the Assets ➤ Scripts
folder and create a new script named RegularLandMinePositions. Open it in MonoDevelop and add
the following code:
#pragma strict
public var prefab : GameObject;
public var xPos : float = -3.5;
public var yPos : float = 8.5;
public var zPos : float = 55.0;
public var numberWidth : int = 8;
public var numberLength : int = 34;
function Start () {
for (var i : int = 0; i < numberLength; i++)
{
for (var j : int = 0; j < numberWidth; j++)
{
var position: Vector3 = Vector3(xPos, yPos, zPos);
Instantiate(prefab, position, Quaternion.identity);
xPos++;
}
xPos = -3.5;
yPos = yPos + 0.05;
zPos = zPos + 1;
}
}
This code breaks down as follows:
(1) public var prefab : GameObject;
Declare the public reference variable prefab of type GameObject to hold the
reference to the prefab that will be used in this script.
1.
(2) public var xPos : float = -3.5;
public var yPos : float = 8.5;
public var zPos : float = 55.0;
Declare the public variables xPos , yPos , and zPos of type float to hold the x, y,
and z coordinates of each instantiated prefab and assign their initial values.
2.
(3) public var numberWidth : int = 8;
public var numberLength : int = 34;
Search WWH ::




Custom Search