Unity TransformPoint、InverseTransformPoint、TransformDirection

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");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值