Transform的坐标变换注意事项
Transform是unity的核心类之一.表示的是物体的平移,旋转和缩放.
而position和localPosition,
分别表示的是,transform的位置是世界空间,和父空间的描述.
注意是 父空间,并不是自身空间.
注意到这点后,在空间的变换时就会省心很多了.
如果想搞清楚transform.position的变换过程,可以这样来测试:
//父空间转世界.Debug.Log(transform.position);Debug.Log(transform.parent.TransformPoint(transform.localPosition));Debug.Log(transform.parent.localToWorldMatrix.MultiplyPoint(transform.localPosition) );
//世界转父空间Debug.Log(transform.localPosition);Debug.Log(transform.parent.InverseTransformPoint(transform.position));Debug.Log(transform.parent.worldToLocalMatrix.MultiplyPoint(transform.position));
Debug.Log(transform.position);Debug.Log(transform.TransformPoint(transform.localPosition));Debug.Log(transform.localToWorldMatrix.MultiplyPoint(transform.localPosition) );
transform.position = target.position;transform.rotation = target.rotation;transform.localScale = target.lossyScale;