I have a custom spawning system that takes defined space from each tile in my random map generator, selects them and places objects in those spaces accordingly. However, at the moment the system is far from accurate. I’m having a lot of trouble getting objects to place properly (and the parent tile is 1x1x1 scale) and I think it has something to do with my calculations. I got it to work partially using a / 2 division but that just says something is really off. The three main pieces of code are:
void populateArea(GameObject obj)
{
string areaType = obj.tag;
float areaX = obj.collider.bounds.size.x;
float areaZ = obj.collider.bounds.size.z;
float posX = obj.transform.position.x;
float posY = obj.transform.position.y;
float posZ = obj.transform.position.z;
Vector3 objLocation = new Vector3(posX, posY, posZ);
switch(areaType)
{
case "Sides":
placeGrass(objLocation, areaX, areaZ);
break;
case "Over":
placeLogs(objLocation, areaX, areaZ);
break;
case "Under":
placeRocks(objLocation, areaX, areaZ);
break;
}
}
Vector3 placeInsideConstrains(Vector3 orgVector, float areaX, float areaZ)
{
Vector3 placeObjec = new Vector3(Random.Range(-areaX,areaX), 0, Random.Range(-areaZ,areaZ));
Vector3 returnVector = orgVector + placeObjec;
return returnVector;
}
void placeLogs(Vector3 originalVector, float areaX, float areaZ)
{
Transform logObj = (Transform)Instantiate(Landscaping[1]);
int randomRotationY = Random.Range (0,360);
logObj.transform.Rotate(new Vector3(0,randomRotationY,0));
logObj.transform.parent = rezzedLand.transform;
logObj.transform.position = placeInsideConstrains(originalVector, areaX, areaZ);
}
placeLogs() is an example of how objects are placed with the system. All of the variables come from the bound gameobjects, being defined by colliders. Over, Under, and Sides are the possible positions within the spawning zones, used to define which objects can be placed where.