Transform.InverseTransformPoint 相反变换位置

function TransformPoint (position : Vector3) : Vector3

Description描述

Transforms position from world space to local space. The opposite of Transform.TransformPoint.

变换位置从世界坐标到自身坐标。和Transform.TransformPoint相反。

Note that the returned position is affected by scale. Use Transform.InverseTransformDirection if you are dealing with directions.

注意,返回位置不受缩放影响。如果你是处理方向使用Transform.InverseTransformDirection

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public Transform cam = Camera.main.transform;
	public Vector3 cameraRelative = cam.InverseTransformPoint(transform.position);
	public void Awake() {
		if (cameraRelative.z > 0)
			print("The object is in front of the camera");
		else
			print("The object is behind the camera");
	}
}
// Calculate the transform's position relative to the camera.
//相对于摄像机计算变换的位置
var cam = Camera.main.transform;
var 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");

• function InverseTransformPoint (x : float, y : float, z : float) : Vector3

Description描述

Note that the returned position is affected by scale. Use Transform.InverseTransformDirection if you are dealing with directions.

Transforms the position x, y, z from world space to local space. The opposite of Transform.TransformPoint.

变换位置 x, y, z从世界坐标到自身坐标。和Transform.TransformPoint相反。

Note that the returned position is affected by scale. Use Transform.InverseTransformDirection if you are dealing with directions.

注意,返回位置不受缩放影响。如果你是处理方向使用Transform.InverseTransformDirection

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public void Awake() {
		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");
	}
}
// Calculate the world origin relative to this transform.
//相对于该transform计算世界原点。
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");
最后修改:2010年12月19日 Sunday 16:51

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。