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.