- Home /
How to move parent when child moves?
So if I have an empty gameobject as parent, and a sphere as its child, and then I add force to the sphere, the sphere will move, but the parent will stay in the same position.
I tried just doing this in the update method:
transform.parent.position = transform.position;
But when I click play, the sphere and its parent just fly out of the place. How should I handle this?
As a rule you don't move parent to child position. Just don't. Notice that children position and rotation is Completely Dependent on it's parent position/rotation so it couldn't happen differently.
Whatever you want to do here just consider is it even necessary. If it still is then share your reasons.
well consider if I move the sphere by adding impulses to it. Once the sphere rotates a bit, its axis are not pointing in the right direction anymore.
If I want to move forward, I will have use a parent to know the right way.
Answer by GrahamReeves · Jun 13, 2014 at 10:10 AM
When you change that parent position, you'll be moving the child. And on the next update, the child has moved more.
child position = parent position + child local offset
so you need to compensate for that, this might be a quick fix
transform.parent.position = transform.position - transform.localpostiion;
Answer by andrew-lukasik · Jun 13, 2014 at 02:40 PM
Sir, you probably need something like this:
void Update () {
Vector3 inputDir = new Vector3(Input.GetAxis("Horizontal"),0f,Input.GetAxis("Vertical"));
Quaternion rotateInputDir = Quaternion.Euler(0f,0f,0f);//rotate me <3
rigidbody.AddForce( rotateInputDir * inputDir * 10f );
}