来自Unity scripting API


  将位置从世界空间转换为局部空间。


但是我不明白下面的代码是如何做到的

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'设置为所提到的转换本地化,这还不够吗?

最佳答案

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;
}

关于c# - InverseTransformPoint如何工作? Unity C#,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49999465/

10-08 20:34