public void generateObjectOnTerrain()
{
objectPool = new ObjectPool(objectToInstantiate, objectsToInstantiate);
float randX = 0f;
float randZ = 0f;
for (int i = 0; i < objectsToInstantiate; i++)
{
if (random == true)
{
//Generate random x,z,y position on the terrain
randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);
}
else
{
}
float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));
var randScaleX = Random.Range(RandScaleMin.x, RandScaleMax.x);
var randScaleY = Random.Range(RandScaleMin.y, RandScaleMax.y);
var randScaleZ = Random.Range(RandScaleMin.z, RandScaleMax.z);
var randVector3 = new Vector3(randScaleX, randScaleY, randScaleZ);
//Apply Offset if needed
yVal = yVal + yOffset;
clonedObject = objectPool.GetInstance();
if (randomScale == true)
clonedObject.transform.localScale = randVector3;//new Vector3(randScaleX, randScaleY, randScaleZ);
if (parent)
clonedObject.transform.parent = this.transform;
clonedObject.tag = "ClonedObject";
clonedObject.transform.position = new Vector3(randX, yVal, randZ);
}
createdObjects = GameObject.FindGameObjectsWithTag(objname);
UpdateList(false, "ClonedObject");
}
这将在地形区域的随机位置实例化对象。
我希望在else中将对象放置在具有两个主要参数/变量的组中是不随机的,一个用于设置对象之间的空间,第二个用于设置如何在行中创建它们,例如,如果在objectsToInstantiate那里是20个对象,我将变量设置为4,另一个变量设置为10,然后对象将是4行,即5行,对象之间的空间将是每边10个。
如果我有55个对象并且我设置为4行,那么将有14行4,最后一行将有3个对象。
例如,连续4个对象的绘画和10个空格中的5行
如果有23个对象,那么它将会是这样的:
答案 0 :(得分:0)
没有尝试,但这应该有效。
int columns = 4;
int space = 10;//set these variables from your parameters
Vector2 CalcPosition(int index) // call this func for all your objects
{
float posX = (index%columns)*space;
float posY = (index/columns)*space;
return new Vector2(posX, posY);
}