How to create a pivot for an object and enable the pivot to move or rotate independently without impacting child object's transformation if needed?

In Unity, I have a setup with a parent object and a child object. The parent object is attached with a script that includes four properties: parentPosition, parentRotation, parentOtherPosition, and parentOtherRotation.

When I modify the parentPosition or parentRotation, the parent object moves or rotates accordingly, and the child object inherits these transformations as expected.

However, when I change the parentOtherPosition or parentOtherRotation, the parent object again moves or rotates, but this time, the child object remains stationary in world space and does not inherit these specific transformations.

public class TestTransform : MonoBehaviour
{
    [SerializeField]
    private Transform parent;
    [SerializeField]
    private Transform child;
  
    public Vector3 parentPos;
    public Vector3 parentRot;
  
    public Vector3 childLocalPos;
    public Vector3 childLocalRot;
  
    public Vector3 parentOtherPos;
    public Vector3 parentOtherRot;
  
    private void LateUpdate()
    {
        parent.position = parentPos + parentOtherPos;
        parent.eulerAngles = parentRot + parentOtherRot;
  
        child.position = parent.TransformPoint(childLocalPos - parentOtherPos);
        child.rotation = parent.rotation * Quaternion.Inverse(Quaternion.Euler(parentOtherRot)) * Quaternion.Euler(childLocalRot);
    }
}

In the hierarchy, although parent and child are distinct game objects and the child is not directly parented to the parent, I aim to exclude the influence of parentOtherPos and parentOtherRot from the final position and rotation of the child. This approach is partially successful. However, when I rotate the parent with parentPos set to any value and subsequently adjust parentOtherPos, both the parent and child objects are unexpectedly moving, instead of only the parent.

Video of partially working solution, my script functions correctly when the parent object’s position is set to zero. However, in the following video, I demonstrate the issue that arises when the parent object is rotated.

Video of the problem, here I want the child to be stationary when parent is being moved.

Try this:

private void LateUpdate()
{
    // Update parent position and rotation
    parent.position = parentPos + parentOtherPos;
    parent.eulerAngles = parentRot + parentOtherRot;

    // Calculate the local position and rotation of the child
    Vector3 localChildPos = parent.InverseTransformPoint(child.position);
    Quaternion localChildRot = Quaternion.Inverse(parent.rotation) * child.rotation;

    // Apply the offset to the local position and rotation
    localChildPos -= parentOtherPos;
    localChildRot = Quaternion.Euler(childLocalRot) * Quaternion.Inverse(Quaternion.Euler(parentOtherRot)) * localChildRot;

    // Update child position and rotation
    child.position = parent.TransformPoint(localChildPos);
    child.rotation = parent.rotation * localChildRot;
}

This should properly handle the offset for both position and rotation, and the child should not be affected by the changes in parentOtherPos and parentOtherRot when you rotate the parent.

Give it a try and let me know if it works!