Click here to Skip to main content
14,932,707 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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;
    Renderer SkyRenderer = landscape.AddComponent<Renderer>();
    var skyCenter =;
    //landscape.transform.position = new Vector3(terrain.bounds.min.x, 50, terrain.bounds.min.z);
    skyCenter = new Vector3(terrain.bounds.min.x, 50, terrain.bounds.min.z);



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?
BillWoodruff 16-Mar-21 19:24pm
single-step through the code, and look at the value of variables: compare what you see with what you expect. that should help you isolate the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900