Version: 2022.3

Transform.InverseTransformPoint

切换到手册
public Vector3 InverseTransformPoint (Vector3 position);

描述

position 从世界空间变换到本地空间。

This function is essentially the opposite of Transform.TransformPoint which is used to convert from local to world space.

注意,返回的位置受缩放影响。如果要处理方向矢量而不是位置,请使用 Transform.InverseTransformDirection

If you need to transform many points at once consider using Transform.InverseTransformPoints instead as it is much faster than repeatedly calling this function.

// Calculate the transform's position relative to the camera.
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);

描述

Transforms the position x, y, z from world space to local space.

This function is essentially the opposite of Transform.TransformPoint which is used to convert from local to world space.

注意,返回的位置受缩放影响。如果要处理方向矢量而不是位置,请使用 Transform.InverseTransformDirection

If you need to transform many points at once consider using Transform.InverseTransformPoints instead as it is much faster than repeatedly calling this function.

// Calculate the world origin relative to this transform.
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 this object"); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961