简体   繁体   中英

Rotate empty object around center

I manually (from editor) create empty object with boxes. It rotate around center. But if I create same object with boxes from script. It rotate around unknown point for me. I found the cause. It happening because boxes has scale (0.1, 0.2, 0.1). If I set (1,1,1) - it work. How to fix it? I want small boxes.

var wall = new GameObject();
    //wall.transform.parent = room.transform;
    wall.transform.name = "Wall";
    wall.transform.position = new Vector3(0,0,0);

    for (int x = -3; x < width-3; x++)
    {
        for (int y = -3; y < height-3; y++)
        {
            var plate = Instantiate(SimplePlate);

            var pos = plate.transform.position;
            float posZ = pos.z - (x * plate.transform.lossyScale.z);
            float posY = pos.y + (y * plate.transform.lossyScale.y);

            var newPos = new Vector3(0, posY, posZ);
            plate.transform.position = newPos;
            plate.transform.parent = wall.transform;
            plate.name = "Plate " + x;
        }
    }

You could create another empty GameObject and stuff your box inside. Then rotate the parent you just created?

Also, how can you rotate an object with a scale of (0,0,0). Did you mean (1,1,1)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM