Unity transform.translate 移动 x 也移动 z

Unity transform.translate move on x also moves z

我正在制作一个更改多个游戏对象的应用程序,除了 cuadro.transform.GetChild(current).transform.position = new Vector3 (0, 0, 0) 移动我的对象外,代码工作正常Z轴300个单位(父GO位置相同)

为什么会这样?

public class ChangePaintScript : MonoBehaviour
{

    public GameObject cuadro;

    private int total;
    private int current = 0;

    private bool changing = false;

    // Use this for initialization
    void Start ( )
    {
        total = cuadro.transform.childCount;
    }

    // Update is called once per frame
    void Update ( )
    {
        if ( Input.GetKeyDown(KeyCode.Space) || changing )
        {
            changing = true;
        }

        if ( changing )
        {
            cuadro.transform.GetChild(current).Translate(new Vector3 (-1500, 0, 0) * Time.deltaTime);

            if ( Mathf.Abs(cuadro.transform.GetChild(current).transform.position.x) > 400 )
            {
                changing = false;

                cuadro.transform.GetChild(current).gameObject.SetActive(false);
                cuadro.transform.GetChild(current).transform.position = new Vector3 (0, 0, 0);
                current++;
                current %= total;
                cuadro.transform.GetChild(current).gameObject.SetActive(true);

            }
        }
    }
}

感谢您的帮助!!!

Why is happening this?

因为 "Translate(vector)" 的工作方式类似于 "transform.position = tramsform position + vector"。如果您的对象在开始移动时的位置为“(0,0,300)”,那么您的目标位置将为“(-1500 * deltatime, 0, 300)”。因此,当您将 "new Vector(0, 0, 0)" 分配给子转换时,您将子转换为 -cuadro.transform.position 值。

所以你可以尝试替换这个:

cuadro.transform.GetChild(current).transform.position = new Vector3 (0, 0, 0);

通过这个:

cuadro.transform.GetChild(current).transform.localPosition= new Vector3 (0, 0, 0);

或者这个:

cuadro.transform.GetChild(current).transform.position= cuadro.transform.position;

希望我正确理解你的问题。