Dtb49
Dtb49

Reputation: 1271

Scaling up a parent and move it so that the child object is at the 0,0,0 point

I feel like this is a noob question but, I can't seem to find an example anywhere. Basically, what I am doing is I have a root object at the zero point and whenever I click on one of its children I scale up the entire object a certain ratio. What I am trying to do next is take the center of the child object that the user clicked on and put it at the zero point but also keeping it in the same local position with the root object.

I thought it would be as simple as:

root.transform.position = root.transform.position - obj.transform.localPosition;

that seems to get it close but, it is not quite the zero point.

EDIT:

To Clarify I want to move the parent but, make it so that the child is at 0,0,0 world position whereas the parent was at the 0,0,0 point previously. Think of it like this:

You have a small car model, the user clicks on the door. The car scales up and the whole car moves so that the door is at the zero point, offset by the distance that the door is away from the center of the car.

Upvotes: 0

Views: 857

Answers (2)

meowgoesthedog
meowgoesthedog

Reputation: 15035

If the parent object itself is not a child of another entity, then simply set its world position to minus the child object's world position:

root.transform.position = -(child.transform.position);

The transformation from local to world position automatically takes into account the scaling.

Upvotes: 1

Hugo Montoya Vazquez
Hugo Montoya Vazquez

Reputation: 32

I'm not sure if I understand completly your question. Do you want to put the child on the 0,0,0 position of the parent or the screen position? or do you want to be moved with the scaling of the parent?

If you want the child to move to the cero coordinates of the parent:

child.transform.localePosition = new Vector3(0,0,0);

Where "child" is the object child inside the object you want to scale.

You can see an example of it here.

Upvotes: 1

Related Questions