Version: 2017.3 (switch to 2017.4)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Transform.InverseTransformPoint

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public method InverseTransformPoint(position: Vector3): Vector3;
public Vector3 InverseTransformPoint(Vector3 position);

Description

Transforms position 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.

Note that the returned position is affected by scale. Use Transform.InverseTransformDirection if you are dealing with direction vectors rather than positions.

#pragma strict
// Calculate the transform's position relative to the camera.
public var cam: Transform;
public var cameraRelative: Vector3;
function Start() {
	cam = Camera.main.transform;
	var cameraRelative: Vector3 = 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");
}
// 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 method InverseTransformPoint(x: float, y: float, z: float): Vector3;
public Vector3 InverseTransformPoint(float x, float y, float z);

Description

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

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

#pragma strict
// Calculate the world origin relative to this transform.
function Start() {
	var relativePoint: Vector3 = 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.
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"); } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答