- Home /
Set child position and move parents with
Hello, I'm creating a game and I have a problem. I have a gameObject with a rigidbody and some script. This gameObject has 2 child and each child has a collider (trigger).
In a script I need to set the position and the rotation of the child and I want that the parent come with the child so the global structure stay the same.
I tried with a the localPosition but nothing work.
Do you get any idea?
Thanks
why don't you position the parent ? Child will move wit it.
Yes but I can't find how to do that. I have a bug I think on the local position.
When I try do : parent.position - (child.position - child.localPosition)
I get 2.0154 but it should be. Any idea?
Can you be a little more specific or post a script of what it is you are trying to achieve.
Answer by Exonto · Dec 29, 2017 at 06:44 PM
Moving or rotating a parent object and its children should be done by transforming the parent. All child objects will automatically transform with the parent. What this means is that you should consider trying to use the parent to move your game object rather than moving the children and then mathematically calculating where the parent should be. You can think of the children as being anchored to the parent, and anything the parent does, its children will do also. This does not work the other way around.
Also, make sure that you are changing the parent's rotation/position correctly in your scripts. Don't do this:
var position = parentTransform.position; // This is a copy of the parent's position
var otherPosition = new Vector(1.0f, 2.0f, 3.0f);
position = position - otherPosition; // Will not change the transform of the parent object
In order to actually set the position, you must instead do:
var otherPosition = new Vector(1.0f, 2.0f, 3.0f);
// Will correctly change the transform
parentTransform.position = parentTransform.position - otherPosition;
Make sure you are setting the position or rotation directly from the object. Don't store it in a variable and expect it to actually transform the game object. Hope this helped.
Can't use this if we want to addForce or Velocity to child. Its like a loop question.
We need physics to work while moving.
But simply changing parent positioning to affects childs won't allow that.
Now if we addForces or velocity to Child, parent position will stay at origin and only child will move.
If we try to set parent's position = child position
Doing this will again affect child's further position.
With transform.position we can move all them together but without any physics happening on child.
If we move child with velocity or forces, it won't affect parent.
Answer by JP_Ferreira · Dec 29, 2017 at 06:45 PM
I don't know if i'm getting it right, but i think you can invert the parent order.
So it would be something like: When you are moving the parent from the child. transform.SetParent(childTransform, true);
When you want to reverse to originalState: childTransform.SetParent(transform.Transform,true);
Didn't test it.