Translate child object according parent position

I’ve made a simple scene with a cube (Parent) and another cube smaller(child) in it.
My goal is to make the child translate according a single axis with a slider value.
When there is no rotation of the parent the translation works fine.
As soon as a rotation is applied to the parent the child follow the rotation but the translation is not good.

public class Translation : MonoBehaviour
    [SerializeField] public UnityEngine.GameObject _Cube;
    [SerializeField] public UnityEngine.GameObject _Child;

    void Start()
        _TranslationSlider.onValueChanged.AddListener(delegate { UpdateTranslation(); });
        _Child.transform.position = new Vector3(_Cube.transform.position.x, _Cube.transform.position.y, _Cube.transform.position.z);

    public void UpdateTranslation()
        _TranslationText.text = _TranslationSlider.value.ToString("0.0") + "mm";
        _Child.transform.position = new Vector3(
            _Cube.transform.position.x ,
            _Cube.transform.position.y ,
            _Cube.transform.position.z - (Single)(Convert.ToSingle(_TranslationSlider.value) / 5.0));


Do you ha tip for this ?


Try to use transform.localPosition, which is position relative to the parent. It’s the value you see in the child’s inspector.
Setting position (worldPosition) is basically computing a localPosition based on parent’s transform.