- Home /
Rotate Parent using Child as Pivot
I am trying to write a script that allow me to rotate an object, treating a dynamically moving child as the pivot.
This is to allow the parent to rotate, while allowing the child to feel as though the rotation was centered on itself.
My current technique is to create a grandparent above the parent, control positional offsets on the grandparent and parent, and then rotate the grandparent when needed. The Grandparent and child should have the same world positions, so that rotating on the child pivot is the same as rotating on the grandparent pivot.
public class TrackedPivotPoint : MonoBehaviour { public Transform parent, child; // Update is called once per frame void Update () { parent.localPosition = -child.localPosition; transform.position = child.localPosition; } }
This script manages the offsets perfectly... as long as the grandparent isn't rotated, which is kind of the point.
I'm not sure what it is im not taking into account at this point.
Any tips where I'm going wrong would be appreciated.
Your answer
Follow this Question
Related Questions
How Do I Give Two Objects the Same Transform? 1 Answer
Make a GameObject a Child of What it Collides With 2 Answers
transform.position not setting position OR animation setting position even though it shouldn't 0 Answers
Why can't I get the transform from an instantiated object? 1 Answer
Distribute terrain in zones 3 Answers