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:
-
Calculate every position in the Beginning → very limiting
-
Move the pivot → also not the solution Im looking for.
-
Bounds → maybe but problematic for rotation
-
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!