Search Unity

Placing and aligning GameObjects within bounds of a parent object?

Discussion in 'Editor & General Support' started by jvhgamer, Apr 25, 2019.

  1. jvhgamer

    jvhgamer

    Joined:
    Jun 27, 2017
    Posts:
    36
    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



    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



    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



    Five (5) objects to place



    Code (CSharp):
    1.     public class NewBehaviourScript : MonoBehaviour {
    2.  
    3.         public int NumberOfObjectsToPlace = 10;   //Place 10 objects
    4.         public int maxRows = 5; // how many objects can be placed in a row
    5.         public int maxColumns = 2; // how many objects can be placed in a column
    6.  
    7.         public GameObject RectangleParent;  //the parentObject instance
    8.         public GameObject CubePrefab;       //the cube prefab
    9.  
    10.         // Use this for initialization
    11.         void Start () {
    12.             PlaceObjects();
    13.         }
    14.  
    15.         // Update is called once per frame
    16.         void Update () {
    17.      
    18.         }
    19.  
    20.         void PlaceObjects()
    21.         {
    22.             Vector3 center = RectangleParent.GetComponent<MeshRenderer>().bounds.center;
    23.             Vector3 size = RectangleParent.GetComponent<MeshRenderer>().bounds.size;
    24.             Vector3 corner = -new Vector3(size.x / 2, 0, size.z / 2);
    25.             float stepX = size.x / (maxColumns + 1);
    26.             float stepZ = size.z / (maxRows + 2);
    27.             int placedObjects = NumberOfObjectsToPlace;
    28.  
    29.             for (int i = 0; i < placedObjects; i++)
    30.             {
    31.                 GameObject Cube = Instantiate(CubePrefab);
    32.                 Cube.transform.parent = RectangleParent.transform;
    33.                 Cube.transform.localRotation = Quaternion.identity;
    34.                 Cube.transform.position = corner + new Vector3(stepX,
    35.                     center.y + 0.15f,
    36.                     stepZ * 1.5f + i * stepZ
    37.                 );
    38.  
    39.                 if (i == maxRows - 1)
    40.                 {
    41.                     i -= maxRows;
    42.                     placedObjects -= maxRows;
    43.                     stepX += size.x / (maxColumns + 1);
    44.                 }
    45.             }
    46.         }
    47.  
    48.     }
     

    Attached Files:

    VirtualLife76 likes this.
  2. jvhgamer

    jvhgamer

    Joined:
    Jun 27, 2017
    Posts:
    36