I'm trying to align 2 game objects using their respective center points at runtime in Unity. The goal is to center my new generated object (SKY) to my map (terrain) which represents the center of my scene. What I already achieved is:
create my game object (terrain)
create 2 float values xCubes and zCubes based on the terrain bounds
create a new Game Object (SKY)
spawning a prefab on each xCubes and zCubes to create the ceiling
Now I need to center SKY on my terrain gameobject with bounds.min.x but the new object results aligned by its origin point, not by its center. SKY has to be aligned by x and z values but not on the Y, for which i need to specify my own value (that's why i cannot use simply bounds.center.
<pre>
private void GenerateCeiling(int xMax, int zMax)
{
GameObject landscape = new GameObject("SKY");
for (int z = 0; z < zMax; z++)
{
for (int x = 0; x < xMax; x++)
{
var newCeiling = Instantiate(skyTile, new Vector3(x * 10, 0, z * 10), Quaternion.Euler(0, 0, 0));
newCeiling.transform.parent = landscape.transform;
}
}
landscape.AddComponent<Renderer>();
Renderer SkyRenderer = landscape.AddComponent<Renderer>();
var skyCenter = SkyRenderer.bounds.center;
skyCenter = new Vector3(terrain.bounds.min.x, 50, terrain.bounds.min.z);
Debug.Log(terrain.bounds.size.x.ToString());
Debug.Log(terrain.bounds.size.z.ToString());
}
What I have tried:
i tried to deal with different renderer.bounds min, max, center, extents etc. and to create new Rendere component on the SKY game Object but it doesn't work.
Anyone can help please?