将 position
从世界空间变换到本地空间。
该函数基本上与 Transform.TransformPoint 相反,后者用于从本地空间转换到世界空间。
注意,返回的位置受缩放影响。如果要处理方向矢量而不是位置,请使用 Transform.InverseTransformDirection。
// 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");
}
}
将位置 z
、z
、z
从世界空间变换到本地空间。与 Transform.TransformPoint 相反。
注意,返回的位置受缩放影响。如果要处理方向,请使用 Transform.InverseTransformDirection。
// 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 of this object");
}
}