Game Development Reference
In-Depth Information
Change the Vector3 pos line to include the offset amounts:
10.
Vector3 pos = new Vector3(x * spacingX + minX + spacingX / 2, y, z * spacingZ + minZ +
spacingZ / 2);
11.
Click Play, and check the positioning (Figure 7-37 ).
Figure 7-37. The veggies auto-planted
With the computer doing all the hard work in your garden, you might want to make use of the code
from the SpawnBunnies script to add some rotation variation. And you may as well do a bit of scale
randomizing as well because the code is very similar.
Add the following code for random rotation beneath the GameObject
newVeggie line:
1.
// assign a random rotation to the clone
Vector3 rot = newVeggie.transform.localEulerAngles; // make a variable to hold the
current local Euler (x,y,z) rotation
rot.y = Random.Range(1,361); // assign a random rotation to the y part of the temporary
variable
newVeggie.transform.localEulerAngles = rot; // assign the new rotation
2.
Save the script, and click Play to see the results.
3.
Add the following code for random scale:
// assign a random scale to the clone
Vector3 scale = newVeggie.transform.localScale; // variable to hold the current local
scale
float rScale = Random.Range(0.5f,1.2f);
scale = new Vector3(rScale,rScale,rScale);
newVeggie.transform.localScale = scale; // assign the new rotation
 
Search WWH ::




Custom Search