transform.localPosition操作时的一些注意事项

移动GameObject是非常平常的一件事情,一下代码看起来很简单:

transform.localPosition += new Vector3 ( 10.0f * Time.deltaTime, 0.0f, 0.0f );

但是小心了,假设上面这个GameObject有一个parent, 并且这个parent GameObject的localScale是(2.0f,2.0f,2.0f)。你的GameObject将会移动20.0个单位/秒。因为该 GameObject的world position等于: 
Vector3 offset = new Vector3( my.localPosition.x * parent.lossyScale.x,
my.localPosition.y * parent.lossyScale.y,
my.localPosition.z * parent.lossyScale.z );
Vector3 worldPosition = parent.position + parent.rotation * offset;
换句话说,上面这种直接操作localPosition的方式是在没有考虑scale计算的时候进行的,为了解决这个问题,Unity3D提供了Translate函数,所以正确的做法应该是: 
transform.Translate ( 10.0f * Time.deltaTime, 0.0f, 0.0f );

原文地址:https://www.cnblogs.com/unity3ds/p/5727063.html