Trying to figure out one formula for positioning rectangles around another rectangle on each axis

Hello everyone,

I have a Question about a Script I’ve written.

I’m Instantiating rectangular shapes around another rectangle (Object1) in given angles, rotated around the y axis. So far it only takes the z-axis scale of object1 and 2 as consideration for the distance between the new object2 (expansion) but since the object1 is also a rectangle, the distance on one axis shouldn’t also be the same as the other one.

Especially for negative values in global space, I yet wasn’t able to figure out a proper solution.
I’ve found following possible solutions already:

  1. Calculate every position in the Beginning → very limiting

  2. Move the pivot → also not the solution Im looking for.

  3. Bounds → maybe but problematic for rotation

  4. Maybe use Rect-Transform → testing, but no experience in implementing for actual 3D Objects.
    Note that the position of Object1 can be different!
    I think I’m looking for a more mathematical approach or there is something I just dont know about or I’m overseeing something and the solution is easier than expected.
    Here is my code and a picture for the general idea:

    foreach (int a in spPosAngles){

    float yrot = obj1.transform.eulerAngles.y;

    Quaternion rotation = Quaternion.Euler(0, yrot + a, 0);
    Debug.Log("rotation : " + rotation);

    //Calculate the position, depending only on the distance from the z axis => need to consider also the x axis scale of the obj1 on the coresponding axis.
    position = obj1.transform.position - (rotation * Vector3.forward *(expansion.transform.localScale.z/ 2 + obj1.transform.localScale.z / 2));

    Vector3 placementpos = new(position.x, 0, position.z);

    direction = new Vector3(obj1.transform.position.x - placementpos.x, 0, obj1.transform.position.z - placementpos.z).normalized;

    Quaternion rotatedVector = Quaternion.LookRotation(direction);

    Instantiate(expansion, placementpos, rotatedVector);

If you need more Information ler me know!

Any hint is appreciated.

Thanks in advance!


To calculate the distance between the two rectangles on both the x and z axes, you can use the scale of both rectangles on those axes. You can do this by multiplying the scale of each rectangle by its corresponding length or width.

For example, if obj1 has a scale of (2, 1, 3) and expansion has a scale of (1, 1, 2), you can calculate the distance between them on the x axis as follows:

float distanceOnXAxis = (obj1.transform.localScale.x / 2) + (expansion.transform.localScale.x / 2);

This will give you the distance between the centers of the two rectangles on the x axis. You can do the same thing for the z axis.

Once you have these values, you can use them to calculate the position of the expansion rectangle relative to obj1. For example:

position.x = obj1.transform.position.x + distanceOnXAxis;
position.z = obj1.transform.position.z + distanceOnZAxis;

This will place the expansion rectangle a distance of distanceOnXAxis and distanceOnZAxis away from obj1 on the x and z axes, respectively.

I hope this helps! Let me know if you have any further questions or if you need further clarification.

I’m not really sure what you are trying to do, but Bounds.Encapsulate [1] could maybe help.

[1] Unity - Scripting API: Bounds.Encapsulate