- Home /
How can I make this spawning system work?
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.
possibly this could help
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
cheers
To be honest I'm not sure if that does apply. They seem to be asking how to keep track of objects. I'm trying to place objects within a specific area defined by boundaries.
Your float variables assigned to transform.position.x,y,z are pointless. Just assign them as their modular float variables as posX, posY, posZ and place them in your Vector3 arguments. Youre kinda reassigning each transform.position when you assign all of the variables into the new Vector3().
You could also use multiple collider objects that trigger different responses ins$$anonymous$$d of trying to code random events from certain sides of the collider.
Answer by Ellandar · Feb 22, 2013 at 09:32 AM
G'day Teonnyn,
Just thinking that it could be your definitions of areaX and areaZ at the start. collider.bounds.size.x and y both return the full X and Z values.
I believe what you may be after is collider.bounds.extents.x and z
Size = full width height
extents = distance from center of x to edge, and similar for Z.
That probably explains why using /2 gets you closer.
Rob.
Source: http://docs.unity3d.com/Documentation/ScriptReference/Bounds.html