Vector3 worldPosition = transform.TransformPoint ( Vector3 relativePosition )
将相对 “当前游戏对象” 的坐标转化为基于世界坐标系的坐标
Vector3 relativePosition = transform.InverseTransformPoint ( Vector3 worldPosition )
将世界坐标转化为相对 “当前游戏对象” 的基于世界坐标系的坐标
Vector3 worldDirection = Transform.TransformDirection ( Vector3 relativeDirection )
将相对 “当前游戏对象” 的方向转化为基于世界坐标系的方向(不受父对象缩放的影响)
Transform.TransformDirection
public Vector3 TransformDirection(Vector3 direction);
Description
Transforms direction
from local space to world space.
This operation is not affected by scale or position of the transform. The returned vector has the same length as direction
.
You should use Transform.TransformPoint for the conversion if the vector represents a position rather than a direction.
public Vector3 TransformDirection(float x, float y, float z);
Description
Transforms direction x
, y
, z
from local space to world space.
This operation is not affected by scale or position of the transform. The returned vector has the same length as direction
.
Transform.TransformPoint
public Vector3 TransformPoint(Vector3 position);
Description
Transforms position
from local space to world space.
Note that the returned position is affected by scale. Use Transform.TransformDirection if you are dealing with direction vectors.
You can perform the opposite conversion, from world to local space using Transform.InverseTransformPoint.
using UnityEngine;
using System.Collections;public class ExampleClass : MonoBehaviour
{
public GameObject someObject;
public Vector3 thePosition; void Start()
{
// Instantiate an object to the right of the current object
thePosition = transform.TransformPoint(Vector3.right * 2);
Instantiate(someObject, thePosition, someObject.transform.rotation);
}
}
public Vector3 TransformPoint(float x, float y, float z);
Description
Transforms the position x
, y
, z
from local space to world space.
Note that the returned position is affected by scale. Use Transform.TransformDirection if you are dealing with directions.
using UnityEngine;
using System.Collections;public class ExampleClass : MonoBehaviour
{
public GameObject someObject; void Start()
{
// Instantiate an object to the right of the current object
Vector3 thePosition = transform.TransformPoint(2, 0, 0);
Instantiate(someObject, thePosition, someObject.transform.rotation);
}
}
Transform.InverseTransformPoint
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.
// 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);
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.
// 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");
}
}