Mark Quinn
Mark Quinn

Reputation: 79

Child position not changing when parent rotates

I feel it is simple but I have been looking for a solution online and cannot find a proper result - apologies if it has already been written.

I have a Sphere and it is slowly rotating on it's Y axis using a simple script:
transform.Rotate(0, Time.deltaTime, 0);

I have a child object attached to this sphere which rotates with the parent. So far so good.

I have placed the pivot to the center of the child and I have tried to get the position of the child object as it is rotated using the parent:

void Update ()
{
    Debug.Log(this.transform.position);
}

However the update provides the position of the child as if it was unattached from the parent.

Would you guys be able to point me in the direction of getting the position of the child when attached to the sphere parent as it updates?

Thank you for your time and patience.

Upvotes: 1

Views: 1387

Answers (1)

Kardux
Kardux

Reputation: 2157

Not sure to really understand what "the position of the child when attached to the sphere parent as it updates" means but I'll give it a try.

I suppose your hierarchy looks like this:

-- Sphere (rotating along its Y axis)
  -- Child (that rotates with "Sphere" parent)
    -- Pivot (in the center of the "Child" object and rotating along its Y axis)
      -- Child2 (that rotates with "Pivot" parent)

If you want to get the position of the Child2 relative to the Sphere, you can do something like this:
Vector3 relativePosition = Child2.transform.position - Sphere.transform.position;

You can also use Transform.localPosition to get the position of an object relative to its parent. In this case you could use it this way:
Vector3 relativePosition = Child.transform.localPosition + Pivot.transform.localPosition + Child2.transform.localPosition

Hope this helps,

Upvotes: 3

Related Questions