移动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 );

相关文章:

  • 2021-11-09
  • 2022-01-10
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-08
  • 2022-12-23
  • 2021-10-23
  • 2021-12-16
  • 2021-12-28
  • 2021-08-09
  • 2021-11-20
相关资源
相似解决方案