Transform是unity的核心类之一.表示的是物体的平移,旋转和缩放.而position和localPosition,分别表示的是,transform的位置是世界空间,和父空间的描述.注意到这点后,在空间的变换时就会省心很多了.如果想搞清楚transform.position的变换过程,可以这样来测试:Java代码
//父空间转世界. Debug.Log(transform.position); Debug.Log(transform.parent.TransformPoint(transform.localPosition)); Debug.Log(transform.parent.localToWorldMatrix.MultiplyPoint(transform.localPosition) );
复制代码
Java代码
//世界转父空间 Debug.Log(transform.localPosition); Debug.Log(transform.parent.InverseTransformPoint(transform.position)); Debug.Log(transform.parent.worldToLocalMatrix.MultiplyPoint(transform.position));
复制代码
Java代码
Debug.Log(transform.position);Debug.Log(transform.TransformPoint(transform.localPosition));Debug.Log(transform.localToWorldMatrix.MultiplyPoint(transform.localPosition) );
复制代码
这时,就应该注意了,transform.localXXX表示的是在父空间的描述.XXXPosition,EulerAngles,Scale)如果想将一个空间的变换信息转换近另一个空间,可以这样:1 最简单的方式Java代码
transform.position = target.position; transform.rotation = target.rotation; transform.localScale = target.lossyScale;
复制代码
,细数门前落叶,倾听窗外雨声,