x


How to changed the center position of child gameObjects / pivot of parent? (Updated)

I am dynamically generating 6 columns and rows of cubes in a loop as children of a gameObject and then setting their transform to parent so I can rotate , move or scale the parent and the children will all move as well. This starts the cube 1 at 0,0 and increments position by cube.x + cube.width. When rotating, this will mean the cubes are moved along the left edge (0,0). I want to make the generated cubes sit at parent.width / 2 and parent.height / 2. Below is an image to demonstrate what I wish to do:-

alt text

Does Unity contain helper methods to adjust the center point of the parent so that the cubes can be moved / scaled on a central point? E.g. centerPoint.x = this.width / 2

Thanks

more ▼

asked Dec 04 '10 at 11:31 AM

boymeetsrobot gravatar image

boymeetsrobot
587 7 10 22

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I'm not sure what you mean exactly, but whatever it is you're wanting to do, you should be able to accomplish it by manipulating the Transform.position and Transform.localPosition fields for the parent and child objects (either in the inspector or via scripting) as needed.

more ▼

answered Dec 04 '10 at 08:40 PM

Jesse Anders gravatar image

Jesse Anders
7.3k 7 17 48

Thanks for the reply Jesse. I have updated the post with an image to make what I am trying to do a bit clearer.

Dec 04 '10 at 09:12 PM boymeetsrobot

I don't know of any shortcuts off the top of my head (aside from manipulating the positions as described previously). Maybe someone else will be able to offer an answer though.

Dec 04 '10 at 10:47 PM Jesse Anders
(comments are locked)
10|3000 characters needed characters left

Have a look at the following script :

var parentGameObject: Transform;
var scale = 0.1; // scale of the cube
var gap = 0.01; // gap between the cubes

function Start()
{
    for(var i = 0; i<6; i++)
    {
        for(var j=0;j<6;j++)
        {
            var cube : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.localScale= Vector3.one*scale;
            cube.transform.position = Vector3(-2.5*scale + (scale+gap)*j, 0, 2.5*scale+ (scale+gap)*i);
            cube.transform.parent = parentGameObject;
        }
    }
}
more ▼

answered Dec 31 '10 at 01:04 AM

uhahaha gravatar image

uhahaha
397 2 3 14

Positions are relative to the parent GO, so apply this script to the parent GO.

Dec 31 '10 at 01:08 AM uhahaha
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2086
x885

asked: Dec 04 '10 at 11:31 AM

Seen: 1741 times

Last Updated: Jun 05 '12 at 01:36 PM