C# Unity transform.translate在x上移动也会移动z
我正在制作一个可以改变几个游戏对象的应用程序,除了cuadro.transform.GetChild(current.transform.position=new Vector3(0,0,0)在Z轴上移动我的对象300个单位(与父GO的位置相同)之外,代码运行良好 为什么会这样C# Unity transform.translate在x上移动也会移动z,c#,unity3d,unity5,translate-animation,C#,Unity3d,Unity5,Translate Animation,我正在制作一个可以改变几个游戏对象的应用程序,除了cuadro.transform.GetChild(current.transform.position=new Vector3(0,0,0)在Z轴上移动我的对象300个单位(与父GO的位置相同)之外,代码运行良好 为什么会这样 public class ChangePaintScript : MonoBehaviour { public GameObject cuadro; private int total; pr
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);
}
}
}
}
谢谢你的帮助
为什么会这样
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);
}
}
}
}
因为“Translate(vector)”的工作原理类似于“transform.position=tramsform position+vector”。如果你的物体在开始移动时的位置是“(0,0300)”,那么你的目标位置将是“(-1500*deltatime,0,300)”。因此,当您将“新向量(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;
希望我能正确理解你的问题。cuadro.transform.GetChild(current)=vector3.zero right?那么你面临的问题是孩子的移动方式与家长的相同?@Chico3001 cuadro游戏对象的位置是什么?家长的位置是0,0300,我必须将其移动到0,0,0以暂时修复它,vector3.zero也失败了。。。