Transform.InverseTransformPoint

Unity · Версия 2020.3 LTS · Scripting · Transform

Transform.InverseTransformPoint

Объявление

public Vector3 InverseTransformPoint(Vector3 position);

Описание

Преобразует position из мирового пространства в локальное пространство.

Эта функция по сути является противоположностью Transform.TransformPoint, которая используется для преобразования локального пространства в мировое.

Обратите внимание, что на возвращаемую позицию влияет масштаб. Используйте Transform.InverseTransformDirection, если вы имеете дело с векторами направления, а не с позициями.

// Рассчитываем положение трансформации относительно камеры.
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public Transform cam;
    public Vector3 cameraRelative;

    void Start()
    {
        cam = Camera.main.transform;
        Vector3 cameraRelative = cam.InverseTransformPoint(transform.position);

        if (cameraRelative.z > 0)
            print("The object is in front of the camera");
        else
            print("The object is behind the camera");
    }
}

Объявление

public Vector3 InverseTransformPoint(float x, float y, float z);

Описание

Преобразует положение x, y, z из мирового пространства к локальному пространству. Противоположность Transform.TransformPoint.

Обратите внимание, что на возвращаемую позицию влияет масштаб. Используйте Transform.InverseTransformDirection, если вы имеете дело с маршрутами.

// Вычислить начало мира относительно этого преобразования.
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void Start()
    {
        Vector3 relativePoint = transform.InverseTransformPoint(0, 0, 0);

        if (relativePoint.z > 0)
            print("The world origin is in front of this object");
        else
            print("The world origin is behind of this object");
    }
}

Обновлено 17 ноября 2026 г.