2

From Unity scripting API

Transforms position from world space to local space.

But I don't understand how it is doing that in the block below

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

Is it making 'pos' local to itself or local to the GameObject that has this script?

Why is the argument for the InverseTransformPoint the variable itself?

Wouldn't it be enought to write pos = transform.InverseTransformPoint(); which would make 'pos' local to the mentioned transform?

1
  • 1
    the argument of the function is the point, supposedly in world space coordinates, from which you want to calculate the local coordinates (the result of the function). The transform on which you apply the function is the local frame of reference, from which the new x, y and z coordinates will be calculated. So in this case, it's replacing "pos" (world coordinates) by local coordinates relative to the current Game Object to which this script is attached.
    – Pac0
    Commented Apr 24, 2018 at 10:41

3 Answers 3

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

Transforms position from world space to local space.

This means that given a Transform object, you will call from the transform to the method InverseTransformPoint with a Vector that represents a position in the world space, this function will return a local space position respect from the object who contains the function, in your case the "transform."

You could be calling it using a child for example:

Vector3 pos = transform.getChild(0).InverseTransformPoint(aWorldPosition);

This would generate the position in local space for the child 0.

-2

If you breakdown the magical code that convert global space/world space position into local space then here is the explanation:

transform.InverseTransformPoint (pos);
  1. transform: transform is representing the object where the script has attached. It is the reference point according to which you want to convert world space into local. It can be any object in the scene.
  2. InverseTransformPoint: The magical function that convert world space position into local space according to step 1 object.
  3. pos:The world space position you want to convert into local space. But which local space? The one you define in step 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.