InverseTransformPoint 是如何工作的?统一 C#
How does InverseTransformPoint work? Unity C#
Transforms position from world space to local space.
但我不明白它在下面的块中是如何做到的
private Vector3 PlaceOnCircle(Vector3 position) {
Ray ray = Camera.main.ScreenPointToRay (position);
Vector3 pos = ray.GetPoint (0f);
// Making 'pos' local to... ?
pos = transform.InverseTransformPoint (pos);
float angle = Mathf.Atan2 (pos.x, pos.z) * Mathf.Rad2Deg;
pos.x = circle.radius * Mathf.Sin (angle * Mathf.Deg2Rad);
pos.z = circle.radius * Mathf.Cos (angle * Mathf.Deg2Rad);
pos.y = 0f;
return pos;
}
它是将 'pos' 设为自身本地还是包含此脚本的 GameObject 本地?
为什么 InverseTransformPoint 的参数是变量本身?
编写 pos = transform.InverseTransformPoint();
会使 'pos' 成为上述转换的局部变量是否足够?
Transforms position from world space to local space.
这意味着给定一个变换 object,您将从变换调用方法 InverseTransformPoint 并使用表示世界中某个位置的向量 space,此函数将 return 包含函数的 object 的本地 space 位置方面,在您的情况下 "transform."
您可以使用 child 来调用它,例如:
Vector3 pos = transform.getChild(0).InverseTransformPoint(aWorldPosition);
这将为 child 0.
在本地 space 中生成位置
private Vector3 PlaceOnCircle(Vector3 position)
{
// Cast a ray from the camera to the given screen point
Ray ray = Camera.main.ScreenPointToRay (position);
// Get the point in space '0' units from its origin (camera)
// The point is defined in the **world space**
Vector3 pos = ray.GetPoint (0f);
// Define pos in the **local space** of the gameObject's transform
// Now, pos can be expressed as :
// pos = transform.right * x + transform.up * y + transform.forward * z
// instead of
// Vector3.right * x + Vector3.up * y + Vector3.forward * z
// You can interpret this as follow:
// pos is now a "child" of the transform. Its coordinates are the local coordinates according to the space defined by the transform
pos = transform.InverseTransformPoint (pos);
// ...
return pos;
}
如果你分解了将全局 space/world space 位置转换为局部 space 的神奇代码,那么这里是解释:
transform.InverseTransformPoint (pos);
transform
: transform 表示脚本所在的对象
随附的。它是您要根据其将世界 space 转换为本地的参考点。它可以是场景中的任何物体。
InverseTransformPoint
:根据步骤1的对象,将世界space位置转换为本地space的神奇函数。
pos
:要转换为本地space的世界space位置。但是哪个本地space?您在步骤 1 中定义的那个。
Transforms position from world space to local space.
但我不明白它在下面的块中是如何做到的
private Vector3 PlaceOnCircle(Vector3 position) {
Ray ray = Camera.main.ScreenPointToRay (position);
Vector3 pos = ray.GetPoint (0f);
// Making 'pos' local to... ?
pos = transform.InverseTransformPoint (pos);
float angle = Mathf.Atan2 (pos.x, pos.z) * Mathf.Rad2Deg;
pos.x = circle.radius * Mathf.Sin (angle * Mathf.Deg2Rad);
pos.z = circle.radius * Mathf.Cos (angle * Mathf.Deg2Rad);
pos.y = 0f;
return pos;
}
它是将 'pos' 设为自身本地还是包含此脚本的 GameObject 本地?
为什么 InverseTransformPoint 的参数是变量本身?
编写 pos = transform.InverseTransformPoint();
会使 'pos' 成为上述转换的局部变量是否足够?
Transforms position from world space to local space.
这意味着给定一个变换 object,您将从变换调用方法 InverseTransformPoint 并使用表示世界中某个位置的向量 space,此函数将 return 包含函数的 object 的本地 space 位置方面,在您的情况下 "transform."
您可以使用 child 来调用它,例如:
Vector3 pos = transform.getChild(0).InverseTransformPoint(aWorldPosition);
这将为 child 0.
在本地 space 中生成位置private Vector3 PlaceOnCircle(Vector3 position)
{
// Cast a ray from the camera to the given screen point
Ray ray = Camera.main.ScreenPointToRay (position);
// Get the point in space '0' units from its origin (camera)
// The point is defined in the **world space**
Vector3 pos = ray.GetPoint (0f);
// Define pos in the **local space** of the gameObject's transform
// Now, pos can be expressed as :
// pos = transform.right * x + transform.up * y + transform.forward * z
// instead of
// Vector3.right * x + Vector3.up * y + Vector3.forward * z
// You can interpret this as follow:
// pos is now a "child" of the transform. Its coordinates are the local coordinates according to the space defined by the transform
pos = transform.InverseTransformPoint (pos);
// ...
return pos;
}
如果你分解了将全局 space/world space 位置转换为局部 space 的神奇代码,那么这里是解释:
transform.InverseTransformPoint (pos);
transform
: transform 表示脚本所在的对象 随附的。它是您要根据其将世界 space 转换为本地的参考点。它可以是场景中的任何物体。InverseTransformPoint
:根据步骤1的对象,将世界space位置转换为本地space的神奇函数。pos
:要转换为本地space的世界space位置。但是哪个本地space?您在步骤 1 中定义的那个。