Unity TransformPoint、InverseTransformPoint、TransformDirection

函数原型

  • Vector3 worldPosition = transform.TransformPoint ( Vector3 relativePosition )
    将相对 “当前游戏对象” 的坐标转化为基于世界坐标系的坐标
  • Vector3 relativePosition = transform.InverseTransformPoint ( Vector3 worldPosition )
    将世界坐标转化为相对 “当前游戏对象” 的基于世界坐标系的坐标
  • Vector3 worldDirection = Transform.TransformDirection ( Vector3 relativeDirection )
    将相对 “当前游戏对象” 的方向转化为基于世界坐标系的方向(不受父对象缩放的影响)

测试代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {

    Vector3 position,p1,p2,p3;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
        Debug.Log("parent(" + transform.name + ")   = " + transform.parent.localPosition + transform.parent.localRotation + transform.parent.localScale);
        Debug.Log("child  (" + transform.name + ")  = " + transform.localPosition + transform.localRotation + transform.localScale);

        position = Vector3.right + Vector3.up;
        Debug.Log("position = " + position);

        p1 = transform.TransformDirection(position);
        p2 = transform.TransformPoint(position);
        p3 = transform.InverseTransformPoint(position);

        Debug.Log("TransformDirection       => " + p1);
        Debug.Log("TransformPoint       => " + p2);
        Debug.Log("InverseTransformPoint    => " + p3);
        Debug.Log("InverseTransformPoint(TransformPoint) => " + transform.InverseTransformPoint(p2));

        Debug.DrawLine(Vector3.zero,        position);
        Debug.DrawLine(Vector3.zero,        p1,                             Color.red);
        Debug.DrawLine(Vector3.zero,        p2,                             Color.green);
        Debug.DrawLine(transform.position,  transform.position + p3,        Color.blue);
        Debug.DrawLine(transform.position,  transform.position + position,  Color.yellow);

        GameObject obj = GameObject.Find("Sphere (1)");
        obj.transform.localPosition = transform.localPosition;
        obj.transform.Translate(p1);
        Debug.DrawLine(transform.position, obj.transform.position);
        Debug.DrawLine(transform.position, transform.position + transform.up, Color.black);

    }

    void OnGUI () {
        int fontSize = 15;
        int halfLen = Vector3.zero.ToString().Length / 4;
        int left = fontSize * halfLen;
        int top = fontSize / 2;
        GUIStyle labelStyle = new GUIStyle(); 
        labelStyle.fontSize = fontSize; 
        Vector3[] wps = new Vector3[]{Vector3.zero, position, p1, p2, transform.position + p3, transform.position + position};
        Color[] colors = new Color[]{Color.white, Color.white, Color.red, Color.green, Color.blue, Color.yellow};
        for(int i=0;i
            Vector3 wp = Camera.main.WorldToScreenPoint(wps[i]);
            labelStyle.normal.textColor = colors[i];
            //屏幕坐标以左下角为原点,而GUI绘图中屏幕坐标以屏幕左上角为原点
            GUI.Label(new Rect(wp.x-left, Screen.height-wp.y-top, 0, 0), wps[i].ToString(), labelStyle);
        }
    }
}

情景模拟

情景一:

Unity TransformPoint、InverseTransformPoint、TransformDirection_第1张图片

情景二:父节点缩放

Unity TransformPoint、InverseTransformPoint、TransformDirection_第2张图片

情景三:父节点旋转

Unity TransformPoint、InverseTransformPoint、TransformDirection_第3张图片

结论

  • TransformPoint 与 InverseTransformPoint 互为逆操作
    transform.InverseTransformPoint(transform.TransformPoint(relativePosition)) = relativePosition
  • TransformPoint 与 InverseTransformPoint 会受到父对象缩放和旋转的影响
  • TransformDirection 始终与相对位置的向量平行,长度相等,且不受缩放的影响

你可能感兴趣的:(Unity)