We have a parent object, RectangleParent, that we want to place X (range of 1-10) number of objects on (floating above) this surface, evenly spaced within the bounds of the parent object from left to right in a maximum of two rows (5 items a row max, two rows max).
Sample package attached (fully operational). Full details, visual and written below incl. comparisons.
How to fix so the placement of our objects, both at origin and away from origin, are within the bounds of their parent?
Current result
ZestyWarmheartedFennecfox
Notice the parent object at origin. It’s children are placed as desired, meeting all criteria. Placed within parent bounds - check. Two rows evenly spaced from each other and within parent bounds - check. Each object is evenly spaced from each other - check.
But then notice, for the object/children shifted away from origin: not placed within parent bounds, spacing between the two rows is too much to fit within the bounds, and the objects are resized/rotated. However the objects within each row are spaced evenly between one another.
If we assign Cube.transform .localPosition instead of .position
PhonyZestyChital
At origin:
The spacing between the rows is still correct but notice now that the spacing between the objects within each row is now incorrect and as well all the objects are not keeping within the parent objects bounds.
Away from origin:
The rows appear to now aligned/rotated correctly and based off the parent objects bounds, but the spacing both between the two rows is incorrect. The two rows are not fitting within the parent objects bound. The child objects themselves have multiple problems. They are disfigured (stretched) and the spacing between one another is incorrect.
Correct example(s):
Ten (10) objects to place
AgreeableOrangeFrog
Five (5) objects to place
FlawedImaginaryAmericanpainthorse
public class NewBehaviourScript : MonoBehaviour {
public int NumberOfObjectsToPlace = 10; //Place 10 objects
public int maxRows = 5; // how many objects can be placed in a row
public int maxColumns = 2; // how many objects can be placed in a column
public GameObject RectangleParent; //the parentObject instance
public GameObject CubePrefab; //the cube prefab
// Use this for initialization
void Start () {
PlaceObjects();
}
// Update is called once per frame
void Update () {
}
void PlaceObjects()
{
Vector3 center = RectangleParent.GetComponent<MeshRenderer>().bounds.center;
Vector3 size = RectangleParent.GetComponent<MeshRenderer>().bounds.size;
Vector3 corner = -new Vector3(size.x / 2, 0, size.z / 2);
float stepX = size.x / (maxColumns + 1);
float stepZ = size.z / (maxRows + 2);
int placedObjects = NumberOfObjectsToPlace;
for (int i = 0; i < placedObjects; i++)
{
GameObject Cube = Instantiate(CubePrefab);
Cube.transform.parent = RectangleParent.transform;
Cube.transform.localRotation = Quaternion.identity;
Cube.transform.position = corner + new Vector3(stepX,
center.y + 0.15f,
stepZ * 1.5f + i * stepZ
);
if (i == maxRows - 1)
{
i -= maxRows;
placedObjects -= maxRows;
stepX += size.x / (maxColumns + 1);
}
}
}
}
4470931–410785–sample.unitypackage (21.8 KB)